Delete partial data in MongoDB?

MongoDBDatabaseBig Data Analytics

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" }
raja
Published on 23-May-2019 12:55:52
Advertisements