Python Mongodb Query Documents

Python Mongodb Python Mongodb

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

#!/usr/bin/python3 import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["exampledb"] mycol = mydb["sites"] x = mycol.find_one() print(x)

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

#!/usr/bin/python3 import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["exampledb"] mycol = mydb["sites"] for x in mycol.find(): print(x)

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

#!/usr/bin/python3 import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["exampledb"] mycol = mydb["sites"] for x in mycol.find({},{ "_id": 0, "name": 1, "alexa": 1 }): print(x)

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

#!/usr/bin/python3 import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["exampledb"] mycol = mydb["sites"] for x in mycol.find({},{ "alexa": 0 }): print(x)

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

#!/usr/bin/python3 import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["exampledb"] mycol = mydb["sites"] for x in mycol.find({},{ "name": 1, "alexa": 0 }): print(x)

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

#!/usr/bin/python3 import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["exampledb"] mycol = mydb["sites"] myquery = { "name": "EXAMPLE" } mydoc = mycol.find(myquery) for x in mydoc: print(x)

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

#!/usr/bin/python3 import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["exampledb"] mycol = mydb["sites"] myquery = { "name": { "$gt": "H" } } mydoc = mycol.find(myquery) for x in mydoc: print(x)

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

#!/usr/bin/python3 import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["exampledb"] mycol = mydb["sites"] myquery = { "name": { "$regex": "^R" } } mydoc = mycol.find(myquery) for x in mydoc: print(x)

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

import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["exampledb"] mycol = mydb["sites"] myresult = mycol.find().limit(3) # Output result for x in myresult: print(x)

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'}

Python Mongodb Python Mongodb

Other Extensions