
- 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
Use ObjectId under findOne() to fetch a specific record in MongoDB?
Let us first create a collection with documents −
> db.findOneWorkingDemo.insertOne({"ClientId":1,"ClientName":"Larry","ClientAge":26}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7c1716d78f205348bc64d") } > db.findOneWorkingDemo.insertOne({"ClientId":2,"ClientName":"Chris","ClientAge":28}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7c17d6d78f205348bc64e") } > db.findOneWorkingDemo.insertOne({"ClientId":3,"ClientName":"Robert","ClientAge":34}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7c1896d78f205348bc64f") }
Following is the query to display all documents from a collection with the help of find() method −
> db.findOneWorkingDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd7c1716d78f205348bc64d"), "ClientId" : 1, "ClientName" : "Larry", "ClientAge" : 26 } { "_id" : ObjectId("5cd7c17d6d78f205348bc64e"), "ClientId" : 2, "ClientName" : "Chris", "ClientAge" : 28 } { "_id" : ObjectId("5cd7c1896d78f205348bc64f"), "ClientId" : 3, "ClientName" : "Robert", "ClientAge" : 34 }
Following is the query to implement findOne() with ObjectId −
> db.findOneWorkingDemo.findOne({"_id":ObjectId("5cd7c17d6d78f205348bc64e")});
This will produce the following output −
{ "_id" : ObjectId("5cd7c17d6d78f205348bc64e"), "ClientId" : 2, "ClientName" : "Chris", "ClientAge" : 28 }
- Related Articles
- MongoDB regular expression to fetch record with specific name “John”, instead of “john”
- MongoDB regular expression to match a specific record?
- Fetch specific field values in MongoDB
- Fetch specific multiple documents in MongoDB
- Query an array in MongoDB to fetch a specific value
- Fetch a specific document in MongoDB with array elements
- Using regex in MongoDB findOne()
- Make nested queries in MongoDB 4 to fetch a specific document
- How to filter dates in MySQL to fetch date record only for a specific month?
- Accessing array values in a MongoDB collection to fetch a specific document
- MongoDB query to find a specific city record from a collection
- How to generate ObjectID in MongoDB?
- Convert string to objectid in MongoDB?
- Fetch a specific record using a single MySQL query with AND & OR operator
- MongoDB aggregation to fetch documents with specific field value?

Advertisements