jwchang
jwchang

Reputation: 10864

Remove only one document in MongoDB

when I call

db.collection.remove({'condition':'some condition'});

This one line will delete all the matching condition documents.

What If I want to remove only one of any or n-th matching condition?

Upvotes: 6

Views: 28184

Answers (9)

Rohit Gaidhane
Rohit Gaidhane

Reputation: 351

I used this and it's worked for me

db.eventList.remove({"_id":ObjectId("5d9f3bd0c02cef7d50bb97fb")});

this is working on MongoDB shell version: 3.2.17

here eventList ===> collection

Upvotes: 1

Ravi Wray
Ravi Wray

Reputation: 169

For example the name of the db is 'abc' and under that there's a collection by name 'users' and users has an email which is '[email protected]' which you want to remove: 1. On the terminal if you type 'show dbs' (without quotation' ') it would list all the databases including 'abc' 2. If you type 'use abc', it would switch to your desired db - abc (with the message: "switched to db abc") 3. Now if you type 'show collections', it would show all the collections including 'users' 4. If you want to list all the documents in this collection, you simply type: db.users.find({}) 5. If you want to delete a specific document in this collection, type: db.users.remove({"email":"[email protected]"})

If it's successful, it would display - WriteResult({ "nRemoved" : 1 })

Upvotes: 0

Ankit Kumar Rajpoot
Ankit Kumar Rajpoot

Reputation: 5590

Try this one for delete.

db.users.deleteOne({"_id": ObjectId("5473293d43ecdre56352457f3a")});

Upvotes: 0

kirthan shetty
kirthan shetty

Reputation: 491

To remove one document from collection use the bellow command

db.collectionname.remove({"_id": ObjectId("5473293d43ecdre56352457f3a")})

Upvotes: 2

According to MongoDB db.collection.remove() reference:

justOne

An optional boolean value. To limit the deletion to just one document, set to true. Omit to use the default value of false and delete all documents matching the deletion criteria.

You can set the justOne to true, in order to do it.

Before MongoDB version 2.6:

db.collection.remove(
   <query>,
   <justOne>
)

MongoDB version 2.6 or later:

db.collection.remove(
   <query>,
   {
     justOne: <boolean>,
     writeConcern: <document>
   }
)

Like this: db.collection.remove({"condition": "some condition"}, {justOne: true});.

Upvotes: 1

maharshi oza
maharshi oza

Reputation: 77

If there are multiple records and you want to delete only first record, then set justOne parameter in remove() method.

Here, you want to delete only 1. Thus, set "justOne" parameter as 1.

db.collection.remove({'condition':'some condition'},1);

Upvotes: 2

arcyqwerty
arcyqwerty

Reputation: 10675

db.collection.remove now has a justOne flag

http://docs.mongodb.org/manual/reference/method/db.collection.remove/#db.collection.remove

Upvotes: 11

RameshVel
RameshVel

Reputation: 65857

You need to perform two separate queries for this

  • take only one item from the matching filters

    var item = db.collection.findOne({'condition':'some condition'})
    
  • and delete the item by using the id

     db.collection.remove({_id: item._id});
    

Upvotes: 18

methodin
methodin

Reputation: 6712

Can't you just run a find first to get the id then a delete when you have the one you want?

Upvotes: 0

Related Questions