This is the 28th day of my participation in the More Text Challenge. For more details, see more text Challenge
If ❤️ my post is helpful, please comment, follow, or like. This is the greatest encouragement for me to continue my technical creation. More past posts are on my personal blog
Query data
All the data that goes into mongodb is stored so that it can be retrieved when it needs to be read.
But instead of reading by a column such as score: read by sort; There will also be I only look at a certain period of time, a certain class of conditional screening; There will also be I want to see the average score of each class aggregate for average…. And so on
These operations can be done with find_one(), find() :
ret2find = collect.find_one()
# {'_id': ObjectId(' 5ea780BF747e3e128470e485 '), 'class_name': 'Student_name ', 'subject':' English ', 'score': 100, 'date': '20200301'}
ret2find = collect.find()
# <pymongo.cursor.Cursor object at 0x0000024BBEBE15C8>
Copy the code
As you can see from the above results, find_one() query results in a single dictionary; Find () is a generator object that can be fetched by traversing for val in ret2find:
Setting search Conditions
But can take out all the data is not enough, query is generally with conditions, even complex conditions – such as: query out of three (1) class, Zhang SAN or Li Si, score more than 90 subjects, how to do?
ret2find = collect.find({"class_name":"Class 1, Senior 3"."score": {"$gt":90},"$or": [{"student_name":"Zhang"}, {"student_name":"Bill"}]})
for val in ret2find:
print(val)
Copy the code
There are two main points:
{“class_name”:” grade “,”score”:{“$gt”:90}}
Grade 3 (1) > 90
In addition to the $gt comparison operator, the table is greater than the meaning.
symbol | meaning |
---|---|
$lt | Less than |
$lte | Less than or equal to |
$gt | Is greater than |
$gte | Greater than or equal to |
$ne | Is not equal to |
$in | Within the scope of |
$nin | Out of range |
{$” or “: [{” student_name” : “* *”}, {” student_name “:” bill “}]}
The student’s name is Zhang SAN or Li Si.
The $or logical operator is used to indicate the relationship between conditions. Other logical operators besides $or include:
symbol | meaning |
---|---|
$and | Take the intersection conditionally |
$not | The opposite set of a single condition |
$nor | The opposite set of conditions |
$or | A union of conditions |
More Query Operations
In addition to the above general operations, we will also use in specific scenarios:
symbol | meaning | The sample | The sample mean |
---|---|---|---|
$regex | Regular match | {“student_name”:{“ “}} |
The student’s name ends in three |
$expr | Aggregate expressions are allowed in queries | {“expr”:{“gt”:[“ budget”]}} |
Query records for overruns that cost more than budgeted |
$exists | Whether the attribute exists | {“date”:{“$exists”: True}} | Date attribute exists |
$exists | Whether the attribute exists | {“date”:{“$exists”: True}} | Date attribute exists |
$type | Type judgment | {“score”:{“$type”:”int”}} | The type of score is int |
$mod | Modulus operation | {‘score’: {‘$mod’: [5, 0]}} | The fraction is modulo 5 and 0 |
More query operators can be found in the official documentation