
- 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
Display MongoDB records by ObjectId?
Let us first create a collection with documents −
> db.findByObjectIdDemo.insertOne({"ClientName":"Larry","ClientAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5cd68cd657806ebf1256f11a") } > db.findByObjectIdDemo.insertOne({"ClientName":"Chris","ClientAge":26}); { "acknowledged" : true, "insertedId" : ObjectId("5cd68cdc57806ebf1256f11b") } > db.findByObjectIdDemo.insertOne({"ClientName":"David","ClientAge":38,"isMarried":true}); { "acknowledged" : true, "insertedId" : ObjectId("5cd68cf657806ebf1256f11c") }
Following is the query to display all documents from a collection with the help of find() method −
> db.findByObjectIdDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd68cd657806ebf1256f11a"), "ClientName" : "Larry", "ClientAge" : 23 } { "_id" : ObjectId("5cd68cdc57806ebf1256f11b"), "ClientName" : "Chris", "ClientAge" : 26 } { "_id" : ObjectId("5cd68cf657806ebf1256f11c"), "ClientName" : "David", "ClientAge" : 38, "isMarried" : true }
Following is the query to find by ObjectId −
> db.findByObjectIdDemo.find({_id: new ObjectId("5cd68cf657806ebf1256f11c")}).toArray(function(err, myDocument) { console.log(myDocument); });
This will produce the following output −
[ { "_id" : ObjectId("5cd68cf657806ebf1256f11c"), "ClientName" : "David", "ClientAge" : 38, "isMarried" : true } ]
- Related Articles
- Display the undefined and exact MongoDB document records
- Convert string to objectid in MongoDB?
- How to generate ObjectID in MongoDB?
- Find a document with ObjectID in MongoDB?
- Display records by grouping dates in MySQL
- MongoDB query to update a MongoDB row with only the objectid
- How to convert ObjectId to string in MongoDB
- MongoDB query to convert from ObjectId to String
- Cast to ObjectId failed for value in MongoDB?
- Create ObjectId in MongoDB using a seed string?
- MongoDB regex to display records whose first five letters are uppercase?
- MySQL query to display records ordered by numeric difference?
- How we can perform sort on ObjectId column in MongoDB?
- MongoDB query to group records and display a specific value with dot notation
- How to return documents of a collection without objectId in MongoDB?

Advertisements