Python Mongodb Query Documents
In MongoDB, the find and find_one methods are used to query data in collections, similar to the SELECT statement in SQL.
The test data used in this article is as follows:

Query One Document
We can usefind_one()method to query one document in a collection.
Querysitesthe first document in the collection:
Example
The output result is:
{'_id': ObjectId('5b23696ac315325f269f28d1'), 'name': 'EXAMPLE', 'alexa': '10000', 'url': 'https://www.example.com'}
Query All Documents in Collection
find()method can query all data in a collection, similar to theSELECT *operation in SQL.
The following example findssitesall documents in the collection:
Example
The output result is:
{'_id': ObjectId('5b23696ac315325f269f28d1'), 'name': 'EXAMPLE', 'alexa': '10000', 'url': 'https://www.example.com'}
{'_id': ObjectId('5b2369cac315325f3698a1cf'), 'name': 'Google', 'alexa': '1', 'url': 'https://www.google.com'}
{'_id': ObjectId('5b236aa9c315325f5236bbb6'), 'name': 'Taobao', 'alexa': '100', 'url': 'https://www.taobao.com'}
{'_id': ObjectId('5b236aa9c315325f5236bbb7'), 'name': 'QQ', 'alexa': '101', 'url': 'https://www.qq.com'}
{'_id': ObjectId('5b236aa9c315325f5236bbb8'), 'name': 'Facebook', 'alexa': '10', 'url': 'https://www.facebook.com'}
{'_id': ObjectId('5b236aa9c315325f5236bbb9'), 'name': '知乎', 'alexa': '103', 'url': 'https://www.zhihu.com'}
{'_id': ObjectId('5b236aa9c315325f5236bbba'), 'name': 'Github', 'alexa': '109', 'url': 'https://www.github.com'}
Query Specific Fields
We can use the find() method to query specific fields, setting the corresponding value of the field to be returned to 1.
Example
The output result is:
{'name': 'EXAMPLE', 'alexa': '10000'}
{'name': 'Google', 'alexa': '1'}
{'name': 'Taobao', 'alexa': '100'}
{'name': 'QQ', 'alexa': '101'}
{'name': 'Facebook', 'alexa': '10'}
{'name': '知乎', 'alexa': '103'}
{'name': 'Github', 'alexa': '109'}
Except for _id, you cannot specify both 0 and 1 in one object. If you set one field to 0, all others are 1, and vice versa.
The following example returns all fields except the alexa field:
Example
The output result is:
{'_id': ObjectId('5b23696ac315325f269f28d1'), 'name': 'EXAMPLE', 'url': 'https://www.example.com'}
{'_id': ObjectId('5b2369cac315325f3698a1cf'), 'name': 'Google', 'url': 'https://www.google.com'}
{'_id': ObjectId('5b236aa9c315325f5236bbb6'), 'name': 'Taobao', 'url': 'https://www.taobao.com'}
{'_id': ObjectId('5b236aa9c315325f5236bbb7'), 'name': 'QQ', 'url': 'https://www.qq.com'}
{'_id': ObjectId('5b236aa9c315325f5236bbb8'), 'name': 'Facebook', 'url': 'https://www.facebook.com'}
{'_id': ObjectId('5b236aa9c315325f5236bbb9'), 'name': '知乎', 'url': 'https://www.zhihu.com'}
{'_id': ObjectId('5b236aa9c315325f5236bbba'), 'name': 'Github', 'url': 'https://www.github.com'}
The following code will report an error if both 0 and 1 are specified:
Example
The error content is roughly as follows:
... pymongo.errors.OperationFailure: Projection cannot have a mix of inclusion and exclusion. ...
Query with Specified Conditions
We can set parameters infind()to filter data.
The following example finds data where the name field is "EXAMPLE":
Example
The output result is:
{'_id': ObjectId('5b23696ac315325f269f28d1'), 'name': 'EXAMPLE', 'alexa': '10000', 'url': 'https://www.example.com'}
Advanced Query
In query condition statements, we can also use modifiers.
The following example reads data where the first letter of the name field has an ASCII value greater than "H". The greater-than modifier condition is{"$gt": "H"} :
Example
The output result is:
{'_id': ObjectId('5b23696ac315325f269f28d1'), 'name': 'EXAMPLE', 'alexa': '10000', 'url': 'https://www.example.com'}
{'_id': ObjectId('5b236aa9c315325f5236bbb6'), 'name': 'Taobao', 'alexa': '100', 'url': 'https://www.taobao.com'}
{'_id': ObjectId('5b236aa9c315325f5236bbb7'), 'name': 'QQ', 'alexa': '101', 'url': 'https://www.qq.com'}
{'_id': ObjectId('5b236aa9c315325f5236bbb9'), 'name': '知乎', 'alexa': '103', 'url': 'https://www.zhihu.com'}
Query Using Regular Expressions
We can also use regular expressions as modifiers.
Regular expression modifiers are only used for searching string fields.
The following example reads data where the first letter of the name field is "R". The regular expression modifier condition is{"$regex": "^R"} :
Example
The output result is:
{'_id': ObjectId('5b23696ac315325f269f28d1'), 'name': 'EXAMPLE', 'alexa': '10000', 'url': 'https://www.example.com'}
Return a Specified Number of Records
If we want to limit the query results to a specified number of records, we can use thelimit()method, which accepts only one numeric parameter.
The following example returns 3 document records:
Example
The output result is:
{'_id': ObjectId('5b23696ac315325f269f28d1'), 'name': 'EXAMPLE', 'alexa': '10000', 'url': 'https://www.example.com'}
{'_id': ObjectId('5b2369cac315325f3698a1cf'), 'name': 'Google', 'alexa': '1', 'url': 'https://www.google.com'}
{'_id': ObjectId('5b236aa9c315325f5236bbb6'), 'name': 'Taobao', 'alexa': '100', 'url': 'https://www.taobao.com'}
Other Extensions
Python Mongodb