
- 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
Delete partial data in MongoDB?
You can use map() for this. Let us first create a collection with documents −
> db.deleteDemo.insertOne({"Name":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd550492cba06f46efe9f06") } > db.deleteDemo.insertOne({"Name":"Carol"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd5504d2cba06f46efe9f07") } > db.deleteDemo.insertOne({"Name":"Sam"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd550512cba06f46efe9f08") } > db.deleteDemo.insertOne({"Name":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd5505d2cba06f46efe9f09") } > db.deleteDemo.insertOne({"Name":"Robert"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd550682cba06f46efe9f0a") } > db.deleteDemo.insertOne({"Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd5506f2cba06f46efe9f0b") } > db.deleteDemo.insertOne({"Name":"Mike"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd550752cba06f46efe9f0c") } > db.deleteDemo.insertOne({"Name":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd5507a2cba06f46efe9f0d") } > db.deleteDemo.insertOne({"Name":"James"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd550822cba06f46efe9f0e") } > db.deleteDemo.insertOne({"Name":"Jace"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd550862cba06f46efe9f0f") }
Following is the query to display all documents from a collection with the help of find() method −
> db.deleteDemo.find();
This will produce the following output −
{ "_id" : ObjectId("5cd550492cba06f46efe9f06"), "Name" : "John" } { "_id" : ObjectId("5cd5504d2cba06f46efe9f07"), "Name" : "Carol" } { "_id" : ObjectId("5cd550512cba06f46efe9f08"), "Name" : "Sam" } { "_id" : ObjectId("5cd5505d2cba06f46efe9f09"), "Name" : "David" } { "_id" : ObjectId("5cd550682cba06f46efe9f0a"), "Name" : "Robert" } { "_id" : ObjectId("5cd5506f2cba06f46efe9f0b"), "Name" : "Chris" } { "_id" : ObjectId("5cd550752cba06f46efe9f0c"), "Name" : "Mike" } { "_id" : ObjectId("5cd5507a2cba06f46efe9f0d"), "Name" : "Bob" } { "_id" : ObjectId("5cd550822cba06f46efe9f0e"), "Name" : "James" } { "_id" : ObjectId("5cd550862cba06f46efe9f0f"), "Name" : "Jace" }
Following is the query to delete partial data in MongoDB −
> var value = db.deleteDemo.find({}, {_id : 1}).skip(5).toArray().map(function(documentValue) { return documentValue._id; }); > db.deleteDemo.remove({_id: {$in:value}}); WriteResult({ "nRemoved" : 5 })
The above query will delete all documents after 5th record. Let us display all documents from the above collection −
> db.deleteDemo.find();
This will produce the following output −
{ "_id" : ObjectId("5cd550492cba06f46efe9f06"), "Name" : "John" } { "_id" : ObjectId("5cd5504d2cba06f46efe9f07"), "Name" : "Carol" } { "_id" : ObjectId("5cd550512cba06f46efe9f08"), "Name" : "Sam" } { "_id" : ObjectId("5cd5505d2cba06f46efe9f09"), "Name" : "David" } { "_id" : ObjectId("5cd550682cba06f46efe9f0a"), "Name" : "Robert" }
- Related Articles
- How to delete partial data in MongoDB?
- How to delete particular data in a document in MongoDB?
- Delete data from a collection in MongoDB using multiple conditions?
- MongoDB - update partial number of documents?
- MongoDB query for Partial Object in an array
- Deleting partial data from a field in MySQL?
- Delete a field and value in MongoDB?
- How to delete multiple ids in MongoDB?
- How to delete a document in MongoDB?
- How to delete everything in a MongoDB database?
- Delete all elements in an array field in MongoDB?
- How to delete Session data in JSP?
- Updating data in MongoDB
- How to delete element from an array in MongoDB?
- How to delete documents from a collection in MongoDB?

Advertisements