Delete partial data in MongoDB?

To delete partial data in MongoDB, you can combine find(), skip(), and map() methods to select specific documents, then use remove() with the $in operator to delete them based on their IDs.

Syntax

var ids = db.collection.find({}, {_id: 1}).skip(N).toArray().map(function(doc) { 
    return doc._id; 
});
db.collection.remove({_id: {$in: ids}});

Sample Data

db.deleteDemo.insertMany([
    {"Name": "John"},
    {"Name": "Carol"},
    {"Name": "Sam"},
    {"Name": "David"},
    {"Name": "Robert"},
    {"Name": "Chris"},
    {"Name": "Mike"},
    {"Name": "Bob"},
    {"Name": "James"},
    {"Name": "Jace"}
]);
{
    "acknowledged": true,
    "insertedIds": [
        ObjectId("5cd550492cba06f46efe9f06"),
        ObjectId("5cd5504d2cba06f46efe9f07"),
        ObjectId("5cd550512cba06f46efe9f08"),
        ObjectId("5cd5505d2cba06f46efe9f09"),
        ObjectId("5cd550682cba06f46efe9f0a"),
        ObjectId("5cd5506f2cba06f46efe9f0b"),
        ObjectId("5cd550752cba06f46efe9f0c"),
        ObjectId("5cd5507a2cba06f46efe9f0d"),
        ObjectId("5cd550822cba06f46efe9f0e"),
        ObjectId("5cd550862cba06f46efe9f0f")
    ]
}

Display all documents to verify the initial data ?

db.deleteDemo.find();
{ "_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" }

Example: Delete All Documents After 5th Record

Delete all documents starting from the 6th position (skip first 5) ?

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 })

Verify Result

Check the remaining documents after deletion ?

db.deleteDemo.find();
{ "_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" }

How It Works

  • find({}, {_id: 1}) − Retrieves only the _id field from all documents
  • skip(5) − Skips the first 5 documents
  • toArray().map() − Converts cursor to array and extracts _id values
  • remove({_id: {$in: ids}}) − Deletes documents whose _id matches any in the array

Conclusion

Use find().skip().map() to collect target document IDs, then remove() with $in operator to delete partial data. This approach provides precise control over which documents to remove based on position or criteria.

Updated on: 2026-03-15T01:13:15+05:30

215 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements