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:
Querying a Single Document
We can use the find_one()
method to query a single document in a collection.
Query the first document in the sites collection:
Example
#!/usr/bin/python3
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["tutorialprodb"]
mycol = mydb["sites"]
x = mycol.find_one()
print(x)
Output:
{'_id': ObjectId('5b23696ac315325f269f28d1'), 'name': 'tutorialpro', 'alexa': '10000', 'url': 'https://www.tutorialpro.org'}
Querying All Documents in a Collection
The find()
method can query all documents in a collection, similar to the SELECT *
operation in SQL.
The following example finds all documents in the sites collection:
Example
#!/usr/bin/python3
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["tutorialprodb"]
mycol = mydb["sites"]
for x in mycol.find():
print(x)
Output:
{'_id': ObjectId('5b23696ac315325f269f28d1'), 'name': 'tutorialpro', 'alexa': '10000', 'url': 'https://www.tutorialpro.org'}
{'_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'}
Querying Specified Fields
We can use the find()
method to query specified fields by setting the corresponding values to 1.
Example
#!/usr/bin/python3
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["tutorialprodb"]
mycol = mydb["sites"]
for x in mycol.find({}, { "_id": 0, "name": 1, "alexa": 1 }):
print(x)
Output:
{'name': 'tutorialpro', 'alexa': '10000'}
{'name': 'Google', 'alexa': '1'}
{'name': 'Taobao', 'alexa': '100'}
{'name': 'QQ', 'alexa': '101'}
{'name': 'Facebook', 'alexa': '10'}
{'name': 'Zhihu', 'alexa': '103'}
{'name': 'Github', 'alexa': '109'}
You cannot specify both 0 and 1 in an object except for _id. If you set one field to 0, the others must be 1, and vice versa.
The following example returns all fields except for the alexa field:
Example
#!/usr/bin/python3
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["tutorialprodb"]
mycol = mydb["sites"]
for x in mycol.find({}, { "alexa": 0 }):
print(x)
The output is:
{'_id': ObjectId('5b23696ac315325f269f28d1'), 'name': 'tutorialpro', 'url': 'https://www.tutorialpro.org'}
{'_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': 'Zhihu', 'url': 'https://www.zhihu.com'}
{'_id': ObjectId('5b236aa9c315325f5236bbba'), 'name': 'Github', 'url': 'https://www.github.com'}
The following code, which specifies both 0 and 1, will throw an error:
Example
#!/usr/bin/python3
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["tutorialprodb"]
mycol = mydb["sites"]
for x in mycol.find({}, { "name": 1, "alexa": 0 }):
print(x)
The error message is likely to be:
...
pymongo.errors.OperationFailure: Projection cannot have a mix of inclusion and exclusion.
...
Query by specified criteria
We can set parameters in find()
to filter the data.
The following example finds data where the name field is "tutorialpro":
Example
#!/usr/bin/python3
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["tutorialprodb"]
mycol = mydb["sites"]
myquery = { "name": "tutorialpro" }
mydoc = mycol.find(myquery)
for x in mydoc:
print(x)
The output is:
{'_id': ObjectId('5b23696ac315325f269f28d1'), 'name': 'tutorialpro', 'alexa': '10000', 'url': 'https://www.tutorialpro.org'}
Advanced queries
In the query condition statement, we can also use modifiers.
This example is used to retrieve data where the ASCII value of the first letter in the "name" field is greater than "H", with the greater-than modifier condition being {"$gt": "H"}
:
Example
#!/usr/bin/python3
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["tutorialprodb"]
mycol = mydb["sites"]
myquery = { "name": { "$gt": "H" } }
mydoc = mycol.find(myquery)
for x in mydoc:
print(x)
Output:
{'_id': ObjectId('5b23696ac315325f269f28d1'), 'name': 'tutorialpro', 'alexa': '10000', 'url': 'https://www.tutorialpro.org'}
{'_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 with Regular Expression
We can also use regular expressions as modifiers.
Regular expression modifiers are only used for string fields.
This example retrieves data where the first letter of the "name" field is "R", with the regular expression modifier condition being {"$regex": "^R"}
:
Example
#!/usr/bin/python3
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["tutorialprodb"]
mycol = mydb["sites"]
myquery = { "name": { "$regex": "^R" } }
mydoc = mycol.find(myquery)
for x in mydoc:
print(x)
Output:
{'_id': ObjectId('5b23696ac315325f269f28d1'), 'name': 'tutorialpro', 'alexa': '10000', 'url': 'https://www.tutorialpro.org'}
Return a Specified Number of Records
If we want to set a specified number of records for the query result, we can use the limit()
method, which only accepts a number parameter.
This example returns 3 document records:
Example
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["tutorialprodb"]
mycol = mydb["sites"]
myresult = mycol.find().limit(3)
# Output result
for x in myresult:
print(x)
Output:
{'_id': ObjectId('5b23696ac315325f269f28d1'), 'name': 'tutorialpro', 'alexa': '10000', 'url': 'https://www.tutorialpro.org'}
{'_id': ObjectId('5b2369cac315325f3698a1cf'), 'name': 'Google', 'alexa': '1', 'url': 'https://www.google.com'}
{'_id': ObjectId('5b236aa9c315325f5236bbb6'), 'name': 'Taobao', 'alexa': '100', 'url': 'https://www.taobao.com'}