Tech C**P
15 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
Get query result from MySQL for last month:

SELECT * FROM users WHERE registration_date >= DATE_SUB(NOW(), INTERVAL 1 MONTH)

#mysql #query #interval #now #date_sub
One of the benefits of Django`'s `QuerySet is that you can apply filters on it, slice it and move it around your classes without actually hitting the database until you do so.

One of the ways that executes the query is using iteration:
for c in Course.objects.all():
print(c.title)
NOTE: the first time you iterate over the query, it hits the database and get the result NOT in each iteration.

In the below ways your query will be evaluated (executed):
- repr
- len
- list (e.g.: `list(Entry.objects.all())`)
- bool

#python #django #querySet #query_set #evaluation
DJANGO

Let's do a little bit more by fetching data from database and pass them to views in Django.

For now we make a change in polls/views.py in index function to get data from Question model and return it as an HTTP response object. At first let's import the model:
from .models import Question


And now in index function write the below code:
latest_question_list = Question.objects.order_by('-pub_date')[:5]
output = ', '.join([q.question_text for q in latest_question_list])
return HttpResponse(output)


The code above gets the last 5 questions and on the second line question_text fields for those que
stions are joined together by comma and finally returned as an HTTP response.

Now if you head over to http://127.0.0.1:8000/polls/ you would see the result of question texts bound together by co
mma.

#python #django #query #django_part9
How to kill a process in mongo shell?

If you are unfamiliar with your current database schema and indexes, or run a heavy query by mistake on production database things go nasty! To find the query and kill it run mongo client as below and run currentOp command:
> mongo
> db.currentOp()

currentOp() database method will display the ongoing queries in json format, find the query which is related to you and get its opid. Now you need
to run killOp() database method to kill that process as below:
db.killOp(229)

229 is the operation id we have got from the first command.

NOTE: terminate running operations with extreme caution. Only use db.killOp() to terminate operations initiated by clients and do not terminate internal database operations.

#mongodb #mongo #killOp #currentOp #query
In order to run a mySQL query from command line you can use -e parameter:

mysql -u <user> -p -e "select * from schema.table"

#mysql #command #query
How do you query is not null in MongoDB?

db.mycollection.find({ 'fieldname' : { $ne: null } });

#database #mongodb #query #ne #null #not_null