M
ONGODB QUERY
Filter the Result
When finding documents in a collection, you can filter the result by using a query object.
The first argument of the find() method is a query object, and is used to limit the search.
Advanced Query
To make advanced queries you can use modifiers as values in the query object.
E.g. to find the documents where the "address" field starts with the letter "S" or higher
(alphabetically), use the greater than modifier: {"$gt": "S"}:
Find documents where the address starts with the letter "S" or higher:
Filter With Regular Expressions
You can also use regular expressions as a modifier.
Regular expressions can only be used to query strings.
To find only the documents where the "address" field starts with the letter "C", use the regular expression {"$regex": "^C"}:
Find documents where the address starts with the letter "C":
ONGODB QUERY
Filter the Result
When finding documents in a collection, you can filter the result by using a query object.
The first argument of the find() method is a query object, and is used to limit the search.
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
for x in mycol.find({"address": "California"},{ "_id": 0 }):
print(x)
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
for x in mycol.find({"address": "California"},{ "_id": 0 }):
print(x)
{'name': 'Kartik', 'address': 'California'}
Advanced Query
To make advanced queries you can use modifiers as values in the query object.
E.g. to find the documents where the "address" field starts with the letter "S" or higher
(alphabetically), use the greater than modifier: {"$gt": "S"}:
Find documents where the address starts with the letter "S" or higher:
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
myquery = { "address": { "$gt": "C" } }
mydoc = mycol.find(myquery)
for x in mydoc:
print(x)
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
myquery = { "address": { "$gt": "C" } }
mydoc = mycol.find(myquery)
for x in mydoc:
print(x)
{'_id': ObjectId('6325d4a1d183dcc6ce300092'), 'name': 'Kartik', 'address': 'California'}
Filter With Regular Expressions
You can also use regular expressions as a modifier.
Regular expressions can only be used to query strings.
To find only the documents where the "address" field starts with the letter "C", use the regular expression {"$regex": "^C"}:
Find documents where the address starts with the letter "C":
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
myquery = { "address": { "$regex": "^C" } }
mydoc = mycol.find(myquery)
for x in mydoc:
print(x)
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
myquery = { "address": { "$regex": "^C" } }
mydoc = mycol.find(myquery)
for x in mydoc:
print(x)
{'_id': ObjectId('6325d4a1d183dcc6ce300092'), 'name': 'Kartik', 'address': 'California'}
Comments
Post a Comment