Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Insert and Update Data - Python MongoDB
Next article icon

MongoDB queries let you filter documents(records) using conditions. The find() method retrieves documents from a collection, and you can pass an optional query to specify which documents to return.

A query is pass as a parameter to filter the results. This query uses key-value pairs and various operators.

Collection.find(query)

Common MongoDB Query Operators

Following is the list of some operators used in the queries in MongoDB:

OperationSyntaxDescription
Equality{"key" : "value"}Matches documents with exact value.
Less Than{ "key" : {$lt:"value"} }Matches values less than the given value.
Greater Than{ "key" : {$gt:"value"} }Matches values greater than the given value.
Less Than Equal to{ "key" : {$lte:"value"} }Matches values less than or equal to the value.
Greater Than Equal to{ "key" : {$gte:"value"} }Matches values greater than or equal to the value.
Not Equal to{ "key" : {$ne: "value"} }Matches all values not equal to the given value.
Logical AND{ "$and" : [ {exp1}, {exp2},..., {expN} ] }Returns documents that match all conditions.
Logical OR{ "$or" : [ {exp1}, {exp2},..., {expN} ] }Returns documents that match at least one condition.
Logical NOT{ "$not" : [ {exp1}, {exp2},..., {expN} ] }Inverts the condition.

Sample Collection used in this article:

Example 1: 

This example filters and displays documents from the collection based on the Quantity field, the first query shows documents with Quantitygreater than 40, while the second shows those with Quantityless than 40.

Python
frompymongoimportMongoClienclient=MongoClient("mongodb://localhost:27017/")db=client["mydatabase"]collection=db["GeeksForGeeks"]cursor=collection.find({"Quantity":{"$gt":40}})print("The data having Quantity greater than 40 is:")fordocincursor:print(doc)cursor=collection.find({"Quantity":{"$lt":40}})print("\nThe data having Quantity less than 40 is:")fordocincursor:print(doc)

Output

Example 2: 

It demonstrates the use of logical operators in MongoDB queries, the first query uses$and to retrieve documents where Quantity satisfies both conditions and the second uses$orto fetch documents where Quantity meets at least one condition.

Python
frompymongoimportMongoClientclient=MongoClient("mongodb://localhost:27017/")db=client["mydatabase"]collection=db["GeeksForGeeks"]cursor=collection.find({"$and":[{"Quantity":{"$gt":40}},{"Quantity":{"$gt":50}}]})print("\nQuantities greater than 40 AND Quantities greater than 40:")fordocincursor:print(doc)cursor=collection.find({"$or":[{"Quantity":{"$gt":40}},{"Quantity":{"$gt":50}}]})print("\nQuantities greater than 40 OR Quantities greater than 40:")fordocincursor:print(doc)

Output

Related Articles:


Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp