
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
Find a document with ObjectID in MongoDB?
To find a document with Objectid in MongoDB, use the following syntax −
db.yourCollectionName.find({"_id":ObjectId("yourObjectIdValue")}).pretty();
To understand the above syntax, let us create a collection with the document. The query to create a collection with a document is as follows −
> db.findDocumentWithObjectIdDemo.insertOne({"UserName":"Larry"}); { "acknowledged" : true, "insertedId" : ObjectId("5c90e4384afe5c1d2279d69b") } > db.findDocumentWithObjectIdDemo.insertOne({"UserName":"Mike","UserAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5c90e4444afe5c1d2279d69c") } > db.findDocumentWithObjectIdDemo.insertOne({"UserName":"Carol","UserAge":26,"UserHobby":["Learning","Photography"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c90e4704afe5c1d2279d69d") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.findDocumentWithObjectIdDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c90e4384afe5c1d2279d69b"), "UserName" : "Larry" } { "_id" : ObjectId("5c90e4444afe5c1d2279d69c"), "UserName" : "Mike", "UserAge" : 23 } { "_id" : ObjectId("5c90e4704afe5c1d2279d69d"), "UserName" : "Carol", "UserAge" : 26, "UserHobby" : [ "Learning", "Photography" ] }
Case 1 − Here is the query to find a document with ObjectId in MongoDB.
> db.findDocumentWithObjectIdDemo.find({"_id":ObjectId("5c90e4704afe5c1d2279d69d")}).pretty();
The following is the output −
{ "_id" : ObjectId("5c90e4704afe5c1d2279d69d"), "UserName" : "Carol", "UserAge" : 26, "UserHobby" : [ "Learning", "Photography" ] }
Case 2 − Here is the query to find another document with ObjectId in MongoDB.
The query is as follows −
> db.findDocumentWithObjectIdDemo.find({"_id": ObjectId("5c90e4444afe5c1d2279d69c")}).pretty();
The following is the output −
{ "_id" : ObjectId("5c90e4444afe5c1d2279d69c"), "UserName" : "Mike", "UserAge" : 23 }
- Related Articles
- MongoDB query to update a MongoDB row with only the objectid
- Find document with array that contains a specific value in MongoDB
- Create ObjectId in MongoDB using a seed string?
- How to generate ObjectID in MongoDB?
- Convert string to objectid in MongoDB?
- Display MongoDB records by ObjectId?
- Find the document by field name with a specific value in MongoDB?
- Find documents with arrays not containing a document with a particular field value in MongoDB?
- Find largest document size in MongoDB?
- Extract a MongoDB document with a specific string
- How to insert a document with date in MongoDB?
- Fetch a specific document in MongoDB with array elements
- How to convert ObjectId to string in MongoDB
- Cast to ObjectId failed for value in MongoDB?
- Search a string with special characters in a MongoDB document?

Advertisements