Python MongoDB Tutorial
Selected Reading
- Python MongoDB - Discussion
- Python MongoDB - Useful Resources
- Python MongoDB - Quick Guide
- Python MongoDB - Limit
- Python MongoDB - Update
- Python MongoDB - Drop Collection
- Python MongoDB - Delete Document
- Python MongoDB - Sort
- Python MongoDB - Query
- Python MongoDB - Find
- Python MongoDB - Insert Document
- Python MongoDB - Create Collection
- Python MongoDB - Create Database
- Python MongoDB - Introduction
- Python MongoDB - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Python MongoDB - Query
Python MongoDB - Query
While retrieving using find() method, you can filter the documents using the query object. You can pass the query specifying the condition for the required documents as a parameter to this method.
Operators
Following is the pst of operators used in the queries in MongoDB.
Operation | Syntax | Example |
---|---|---|
Equapty | {"key" : "value"} | db.mycol.find({"by":"tutorials point"}) |
Less Than | {"key" :{$lt:"value"}} | db.mycol.find({"pkes":{$lt:50}}) |
Less Than Equals | {"key" :{$lte:"value"}} | db.mycol.find({"pkes":{$lte:50}}) |
Greater Than | {"key" :{$gt:"value"}} | db.mycol.find({"pkes":{$gt:50}}) |
Greater Than Equals | {"key" {$gte:"value"}} | db.mycol.find({"pkes":{$gte:50}}) |
Not Equals | {"key":{$ne: "value"}} | db.mycol.find({"pkes":{$ne:50}}) |
Example1
Following example retrieves the document in a collection whose name is sarmista.
from pymongo import MongoCpent #Creating a pymongo cpent cpent = MongoCpent( localhost , 27017) #Getting the database instance db = cpent[ sdsegf ] #Creating a collection coll = db[ example ] #Inserting document into a collection data = [ {"_id": "1001", "name": "Ram", "age": "26", "city": "Hyderabad"}, {"_id": "1002", "name": "Rahim", "age": "27", "city": "Bangalore"}, {"_id": "1003", "name": "Robert", "age": "28", "city": "Mumbai"}, {"_id": "1004", "name": "Romeo", "age": "25", "city": "Pune"}, {"_id": "1005", "name": "Sarmista", "age": "23", "city": "Delhi"}, {"_id": "1006", "name": "Rasajna", "age": "26", "city": "Chennai"} ] res = coll.insert_many(data) print("Data inserted ......") #Retrieving data print("Documents in the collection: ") for doc1 in coll.find({"name":"Sarmista"}): print(doc1)
Output
Data inserted ...... Documents in the collection: { _id : 1005 , name : Sarmista , age : 23 , city : Delhi }
Example2
Following example retrieves the document in a collection whose age value is greater than 26.
from pymongo import MongoCpent #Creating a pymongo cpent cpent = MongoCpent( localhost , 27017) #Getting the database instance db = cpent[ ghhj ] #Creating a collection coll = db[ example ] #Inserting document into a collection data = [ {"_id": "1001", "name": "Ram", "age": "26", "city": "Hyderabad"}, {"_id": "1002", "name": "Rahim", "age": "27", "city": "Bangalore"}, {"_id": "1003", "name": "Robert", "age": "28", "city": "Mumbai"}, {"_id": "1004", "name": "Romeo", "age": "25", "city": "Pune"}, {"_id": "1005", "name": "Sarmista", "age": "23", "city": "Delhi"}, {"_id": "1006", "name": "Rasajna", "age": "26", "city": "Chennai"} ] res = coll.insert_many(data) print("Data inserted ......") #Retrieving data print("Documents in the collection: ") for doc in coll.find({"age":{"$gt":"26"}}): print(doc)
Output
Data inserted ...... Documents in the collection: { _id : 1002 , name : Rahim , age : 27 , city : Bangalore } { _id : 1003 , name : Robert , age : 28 , city : Mumbai }Advertisements