
- 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
Removing _id element from PyMongo results?
To remove the _id element, you can use the following syntax −
db.yourCollectionName.find({},{'_id': false}).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.removingidElementDemo.insertOne({"UserName":"John", ... "UserAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5c9153fd4afe5c1d2279d6ad") } > db.removingidElementDemo.insertOne({"UserName":"Carol", "UserAge":24}); { "acknowledged" : true, "insertedId" : ObjectId("5c9154084afe5c1d2279d6ae") } > db.removingidElementDemo.insertOne({"UserName":"David", "UserAge":22}); { "acknowledged" : true, "insertedId" : ObjectId("5c9154154afe5c1d2279d6af") } > db.removingidElementDemo.insertOne({"UserName":"Mike", "UserAge":26}); { "acknowledged" : true, "insertedId" : ObjectId("5c9154204afe5c1d2279d6b0") } > db.removingidElementDemo.insertOne({"UserName":"Chris", "UserAge":20}); { "acknowledged" : true, "insertedId" : ObjectId("5c91542c4afe5c1d2279d6b1") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.removingidElementDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c9153fd4afe5c1d2279d6ad"), "UserName" : "John", "UserAge" : 21 } { "_id" : ObjectId("5c9154084afe5c1d2279d6ae"), "UserName" : "Carol", "UserAge" : 24 } { "_id" : ObjectId("5c9154154afe5c1d2279d6af"), "UserName" : "David", "UserAge" : 22 } { "_id" : ObjectId("5c9154204afe5c1d2279d6b0"), "UserName" : "Mike", "UserAge" : 26 } { "_id" : ObjectId("5c91542c4afe5c1d2279d6b1"), "UserName" : "Chris", "UserAge" : 20 }
Here is the query to remove an _id element from PyMongo −
> db.removingidElementDemo.find({},{'_id': false}).pretty();
The following is the output in which you cannot see the _id element since we have removed it −
{ "UserName" : "John", "UserAge" : 21 } { "UserName" : "Carol", "UserAge" : 24 } { "UserName" : "David", "UserAge" : 22 } { "UserName" : "Mike", "UserAge" : 26 } { "UserName" : "Chris", "UserAge" : 20 }
- Related Articles
- Removing the Min Element from Deaps
- Removing the Min Element from Interval Heaps
- Removing an array element from a MongoDB collection
- Removing an element from an Array in Javascript
- Removing an element from C++ std::vector by index?
- Removing the specified element from the List in C#
- Get an element from a Stack in Java without removing it
- Removing an element from the end of the array in Javascript
- Removing an element from the start of the array in javascript
- Removing an array element from MongoDB collection using update() and $pull
- Removing an element from a given position of the array in Javascript
- Performing regex Queries with PyMongo?
- Get a single element from the array of results by index in MongoDB
- Removing the odd occurrence of any number/element from an array in JavaScript
- K-th smallest element after removing some integers from natural numbers in C++

Advertisements