
- 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
Can we remove _id from MongoDB query result?
To remove _id from MongoDB result, you need to set 0 for _id field. Following is the syntax
db.yourCollectionName.find({},{_id:0});
To understand it, let us create a collection with documents. Following is the query
> db.removeIdDemo.insertOne({"UserName":"John","UserAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5c9bb4042d66697741252440") } > db.removeIdDemo.insertOne({"UserName":"Mike","UserAge":27}); { "acknowledged" : true, "insertedId" : ObjectId("5c9bb40c2d66697741252441") } > db.removeIdDemo.insertOne({"UserName":"Sam","UserAge":34}); { "acknowledged" : true, "insertedId" : ObjectId("5c9bb4162d66697741252442") } > db.removeIdDemo.insertOne({"UserName":"Carol","UserAge":29}); { "acknowledged" : true, "insertedId" : ObjectId("5c9bb4222d66697741252443") }
Following is the query to display all documents from a collection with the help of find() method
> db.removeIdDemo.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5c9bb4042d66697741252440"), "UserName" : "John", "UserAge" : 23 } { "_id" : ObjectId("5c9bb40c2d66697741252441"), "UserName" : "Mike", "UserAge" : 27 } { "_id" : ObjectId("5c9bb4162d66697741252442"), "UserName" : "Sam", "UserAge" : 34 } { "_id" : ObjectId("5c9bb4222d66697741252443"), "UserName" : "Carol", "UserAge" : 29 }
Following is the query to remove _id from Mongo result
> db.removeIdDemo.find({},{_id:0});
This will produce the following output
{ "UserName" : "John", "UserAge" : 23 } { "UserName" : "Mike", "UserAge" : 27 } { "UserName" : "Sam", "UserAge" : 34 } { "UserName" : "Carol", "UserAge" : 29 }
- Related Articles
- Can we work MongoDB findOne() with long type _id?
- MongoDB query to group by _id
- MongoDB query to remove item from array?
- MongoDB query to remove subdocument from document?
- MongoDB query to remove entire array from collection?
- Can MongoDB find() function display avoiding _id?
- MongoDB query to display all the fields value, except _id
- MongoDB query to remove array elements from a document?
- MongoDB query to remove entire data from a collection
- How to remove document on the basis of _id in MongoDB?
- MongoDB query to match and remove element from an array?
- MongoDB query to remove element from array as sub property
- How can we remove corruption from India?
- Retrieving specific documents from collection by _id in MongoDB
- Can MongoDB return result of increment?

Advertisements