
- 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
How to return documents of a collection without objectId in MongoDB?
To return documents of a collection without objectId, set _id:0. Let us first create a collection with documents −
> db.returnDocumentWithoutObjectId.insertOne({"Name":"Carol","Age":25}); { "acknowledged" : true, "insertedId" : ObjectId("5ce8ba6c78f00858fb12e8fa") } > db.returnDocumentWithoutObjectId.insertOne({"Name":"Sam","Age":21}); { "acknowledged" : true, "insertedId" : ObjectId("5ce8ba6d78f00858fb12e8fb") } > db.returnDocumentWithoutObjectId.insertOne({"Name":"John","Age":23}); { "acknowledged" : true, "insertedId" : ObjectId("5ce8ba6f78f00858fb12e8fc") }
Following is the query to display all documents from a collection with the help of find() method −
> db.returnDocumentWithoutObjectId.find();
This will produce the following output −
{ "_id" : ObjectId("5ce8ba6c78f00858fb12e8fa"), "Name" : "Carol", "Age" : 25 } { "_id" : ObjectId("5ce8ba6d78f00858fb12e8fb"), "Name" : "Sam", "Age" : 21 } { "_id" : ObjectId("5ce8ba6f78f00858fb12e8fc"), "Name" : "John", "Age" : 23 }
Following is the query to return documents of a collection without objectId in MongoDB −
> db.returnDocumentWithoutObjectId.find({},{_id:0});
This will produce the following output −
{ "Name" : "Carol", "Age" : 25 } { "Name" : "Sam", "Age" : 21 } { "Name" : "John", "Age" : 23 }
- Related Articles
- How to delete documents from a collection in MongoDB?
- How to retrieve documents from a collection in MongoDB?
- How to count the number of documents in a MongoDB collection?
- How to find two random documents in a MongoDB collection of 6?
- How to sort the documents of a MongoDB collection using java?
- How to delete all the documents from a collection in MongoDB?
- How to Update multiple documents in a MongoDB collection using Java?
- How to update or modify the existing documents of a collection in MongoDB?
- Limit the number of documents in a collection in MongoDB?
- How to generate ObjectID in MongoDB?
- How to insert new documents into a MongoDB collection in your database?
- How to add a new field to all the documents in a MongoDB collection
- How to sum the value of a key across all documents in a MongoDB collection?
- How to perform intersection of sets between the documents in a single collection in MongoDB?
- How to retrieve all the documents from a MongoDB collection using Java?

Advertisements