How to Search for an Object by its ObjectId in the Mongo Console?
Last Updated :
18 Apr, 2024
In MongoDB, every document has a its unique ObjectId that acts as its primary key. ObjectId helps to search for an object using its ObjectId can be incredibly useful when managing and interacting with your MongoDB databases.
In this article, we will explore How to search for an object by its ObjectId in the Mongo console By the end of this guide, you will be confident in your ability to search for and retrieve objects using their ObjectIds in the MongoDB console.
How to Search for an Object by its ObjectId?
When working with MongoDB process of searching for an object by its ObjectId in the MongoDB console. ObjectId is a unique identifier for every document in a MongoDB collection and knowing how to search for an object using its ObjectId can have multiple approaches. Below are the approaches that help us How to Search for an Object by its ObjectId:
- Using the find() method
- Using the findOne() method
Let's set up an Environment:
To understand How to search for an object by its ObjectId we need a collection and some documents on which we will perform various queries. Here we will consider a collection called courses which contains information like course name, Instructor name, fees, and duration. of the courses in various documents.
COURSES1. Using the find() method
The find() method in MongoDB is used to retrieve documents from a collection. It is one of the most commonly used methods in MongoDB.
Syntax:
db.collection.find(query, projection)
- query: is an optional parameter that specifies the query filter.
- projection: is an optional parameter that specifies which fields to include in the returned documents.
Example 1: Get the Java Course Details
Query:
var objectId= ObjectId("6616b9f157bac1647326e11d")
db.courses.find({_id: objectId})
Output:

The above query creates a variable objectId and assigns it the value of a MongoDB ObjectId "6616b9f157bac1647326e11d", and then it uses the find() method to retrieve a document from the courses collection where the _id field matches the objectId value.
Example 2: Get the Python Course Details
Query:
var objectId= ObjectId("6616b9f157bac1647326e11e")
db.courses.find({_id: objectId})
Output:

The above query creates a variable objectId and assigns it the value of a MongoDB ObjectId "6616b9f157bac1647326e11e", and then it uses the find() method to retrieve a document from the courses collection where the _id field matches the objectId value.
2. Using the findOne() method
The findOne() method in MongoDB is used to retrieve a single document from a collection that matches the specified query. It returns the first document that matches the query filter or null if no documents match the filter.
Syntax:
db.collection.findOne(query, projection)
- query: is an optional parameter that specifies the query filter.
- projection: is an optional parameter that specifies which fields to include in the returned document.
Example 1: Get the Java Course Details
Query:
var objectId= ObjectId("6616b9f157bac1647326e11d")
db.courses.findOne({_id: objectId})
Output:

The above query creates a variable objectId with the value of a MongoDB ObjectId "6616b9f157bac1647326e11d", and then uses the findOne() method to retrieve the first document from the courses collection where the _id field matches the objectId value.
Example 2: Get the Python Course Details
Query:
var objectId= ObjectId("6616b9f157bac1647326e11e")
db.courses.findOne({_id: objectId})
Output:

The above query creates a variable objectId with the value of a MongoDB ObjectId "6616b9f157bac1647326e11e", and then uses the findOne() method to retrieve the first document from the courses collection where the _id field matches the objectId value.
Conclusion
Overall in summary , findOne() returns a single document, while find() returns a cursor object that can be used to iterate over multiple documents. The findOne() method is useful when you know that only one document matches the specified query, or when you only need to retrieve a single document. Suppose if there are multiple documents in the collection that match the query filter, the findOne() method returns only the first one. If you want to retrieve all the documents that match the filter, you should use the find() method instead.
Similar Reads
How to Print to Console an Object in a MongoDB Script?
MongoDB queries can sometimes be slow, especially when dealing with large datasets or complex queries. So there are some methods or approaches that allow the developers to check the data and spot mistakes during the program run. In this article, we will learn about How to Print to Console an Object
3 min read
How to Converting ObjectId to String in MongoDB
In MongoDB, documents are uniquely identified by a field called ObjectId. While ObjectId is a unique identifier for each document there may be scenarios where we need to convert it to a string format for specific operations or data manipulation. In this article, we'll learn about the process of conv
4 min read
How to Retrieve only the Queried Element in an Object Array in MongoDB Collection
In MongoDB, retrieving specific elements from an object array within a document is a common requirement, especially when dealing with complex data structures. MongoDB provides powerful query operators to filter and retrieve only the elements that match certain criteria within an array. This capabili
4 min read
How to check if a string is valid MongoDB ObjectId in Node.js ?
Checking if a string is valid mongoDB ObjectID is important while working with databases. It ensures accurate reference the the objects in databases. We can validate the ObjectId with the help of isValid function in MongoDB.Prerequisites:NodeJS and NPM installedMongoose and MongoDBTable of ContentMo
3 min read
How to List All Collections in the MongoDB Shell?
Managing collections is a fundamental task in MongoDB database administration. Knowing how to list all collections in the MongoDB shell is essential for understanding your database structure and managing your data effectively. In this article, we'll explore how to list all collections in the MongoDB
3 min read
How to Creating Mongoose Schema with an Array of ObjectID
In Mongoose, a powerful MongoDB object modeling tool for Node.js, schemas define the structure of documents within a collection. Sometimes, you may need to create a schema with an array of ObjectIDs to represent relationships between documents in different collections. This article will explain how
4 min read
How to Query MongoDB ObjectId by Date?
MongoDB ObjectId is an important element in document identification, but can it help us with date-based queries? Understanding the ObjectId's structure and its potential for querying by date opens up new possibilities for data retrieval and analysis. In this article, We will learn about how to perfo
4 min read
How to do a Full-Text Search in MongoDB using Mongoose
In MongoDB, performing a full-text search allows us to query and retrieve documents based on textual content matching certain criteria. When using Mongoose, an ODM (Object Data Modeling) library for MongoDB in Node.js, conducting full-text search operations can be efficiently achieved using the Mong
5 min read
How to add range in the Collection of Mongodb using Node.js ?
Mongoose.module is one of the most powerful external module of the node.js.Mongoose is a MongoDB ODM i.e (Object database Modelling) that used to translate the code and its representation from MongoDB to the Node.js server.Mongoose module provides several functions in order to manipulate the documen
2 min read
How to Get only the Objecte of Document in MongoDB
In MongoDB, the ability to extract specific objects from documents is an important skill that can significantly enhance our data retrieval capabilities. MongoDB's flexible document model allows for nested structures, where documents can contain arrays or sub-documents. However, retrieving only the d
3 min read