Skip to content
Related Articles

Related Articles

Search by ObjectId in mongodb with PyMongo – Python
  • Last Updated : 03 Jan, 2021

MongoDB is NoSQL Document-oriented database. In MongoDB, the data are stored in JSON like document structure instead of storing data in columns and rows manner. MongoDB is flexible for storing semi-structured and unstructured data.  MongoDB is used for high volume data storage and can scale horizontally.

ObjectId : ObjectId class is a 12 byte binary BSON type, in which 4 bytes of timestamp of creation, 5 bytes of random value and 3 bytes of incrementing counter. ObjectId is the default primary key for MongoDB document and usually found in “_id” field.

eg : { "_id" : ObjectId("54759eb3c090d83494e2d804") }

PyMongo : PyMongo is a native Python driver for MongoDB. It allows interaction with MongoDB Database through Python.

Step 1: Make sure to start the MongoDB database locally on the default port (27017). Connecting to MongoDB, accessing the database, and collecting objects.


# importing MongoClient from pymongo
from pymongo import MongoClient
# importing ObjectId from bson library
from bson.objectid import ObjectId
# Establishing connection with
# mongodb on localhost
client = MongoClient('', 27017)
# Access database object
db = client['database']
# Access collection object
collection = db['collection']

Step 2: 

Querying MongoDB with find_one(). This function accepts the query as their first parameter and filter as the second. The first two methods are implemented using the only query with ObjectId. The last method is implemented using query and filters. 

Example 1: In this example, we have already stored the ObjectId object instance, then use the stored object instance to search for the document.


# inserts data into collection and
# returns an object of type objectId
objInstance = collection.insert_one({"name": "sam", "age": 20}).inserted_id
# search MongoDB with
# an object of type objectId


Example 2: In this example, we have the hex string not an object of ObjectId. We import ObjectId from the bson library to search the document.


# use the string object id
# and objectId object to
# create object of type ObjectId
id = "5fec2c0b348df9f22156cc07"
objInstance = ObjectId(id)
collection.find_one({"_id": objInstance})
# below line works same as the above
collection.find_one({"_id": ObjectId(id)})


Example 3: In this example, we advance the search with filter options. The below filter will return the entire document without the “_id” field.


# search document with filter
# firat parameter is search queary
# second paramater is filter queary
# you can as many fields you want
# you can specify either 1 or 0
# in filter fields
# if you specify 0 the field will
# be eleminated form the result
queary = {"_id": ObjectId(id)}
filter = {"_id": 0}
collection.find_one(queary, filter)


Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :