Remove by _id in MongoDB console

Mongodb

Mongodb Problem Overview


In the MongoDB console how can I remove a record by id? Here's my collection :

[ 
  {
     "_id" : { "$oid" : "4d512b45cc9374271b02ec4f" },
     "name" : "Gazza"
  },
  {
     "_id" : { "$oid" : "4d513345cc9374271b02ec6c" },
     "name" : "Dave",
     "adminOf" : { },
     "email" : "[email protected]"
  }
]

And here are the commands I've tried that don't work :

db.test_users.remove( {"_id":{"$oid":new ObjectId("4d512b45cc9374271b02ec4f")}});
db.test_users.remove( {"_id":{"$oid":"4d513345cc9374271b02ec6c"}});
db.test_users.remove( {"_id":"4d512b45cc9374271b02ec4f"});
db.test_users.remove( {"_id":new ObjectId("4d512b45cc9374271b02ec4f")});

Removing by name works :

db.test_users.remove( {"name":"Gazza"});

This is in the browser shell on at mongodb.org if that makes any difference

Thanks

Mongodb Solutions


Solution 1 - Mongodb

Very close. This will work:

db.test_users.deleteOne( {"_id": ObjectId("4d512b45cc9374271b02ec4f")});

i.e. you don't need a new for the ObjectId.

Also, note that in some drivers/tools, remove() is now deprecated and deleteOne or deleteMany should be used instead.

Solution 2 - Mongodb

If you would like to remove by a list of IDs this works great.

db.CollectionName.remove({
    "_id": {
        $in: [
            ObjectId("0930292929292929292929"),
            ObjectId("0920292929292929292929")
        ]
     }
}) 

    

Solution 3 - Mongodb

Well, the _id is an object in your example, so you just need to pass an object

'db.test_users.remove({"_id": { "$oid" : "4d513345cc9374271b02ec6c" }})'

This should work

Edit: Added trailing paren to ensure that it compiled.

Solution 4 - Mongodb

The answer is that the web console/shell at mongodb.org behaves differently and not as I expected it to. An installed version at home worked perfectly without problem ie; the auto generated _id on the web shell was saved like this :

"_id" : { "$oid" : "4d512b45cc9374271b02ec4f" },

The same document setup at home and the auto generated _id was saved like this :

"_id" : ObjectId("4d5192665777000000005490")

Queries worked against the latter without problem.

Solution 5 - Mongodb

Do you have multiple mongodb nodes in a replica set?

I found (I am using via Robomongo gui mongo shell, I guess same applies in other cases) that the correct remove syntax, i.e.

db.test_users.remove({"_id": ObjectId("4d512b45cc9374271b02ec4f")})

...does not work unless you are connected to the primary node of the replica set.

Solution 6 - Mongodb

I've just bumped into this myself and this variation worked for me:

db.foo.remove({**_id**: new ObjectId("4f872685a64eed5a980ca536")})

Solution 7 - Mongodb

first get the ObjectID function from the mongodb ObjectId = require(mongodb).ObjectID;

then you can call the _id with the delete function

"_id" : ObjectId("4d5192665777000000005490")

Solution 8 - Mongodb

db.collection("collection_name").deleteOne({_id:ObjectId("4d513345cc9374271b02ec6c")})

Solution 9 - Mongodb

Even though this post is outdated, collection.remove is deprecated! collection.delete_one should be used instead!

More information can be found here under #remove

Solution 10 - Mongodb

Suppose we have this dummy collection:

{ "_id" : ObjectId("5ea53fedaa79db20d4e14284"), "item" : "planner", "qty" : 75 }

simply use:

db.inventory.deleteOne({ _id: ObjectId("5ea53fedaa79db20d4e14284") })

it will be deleted with this as a response:

{ "acknowledged" : true, "deletedCount" : 1 }

Thats it.

Solution 11 - Mongodb

db.getCollection("test_users").deleteOne({_id: new ObjectId("4d512b45cc9374271b02ec4f")})

Attribution : To be fair this was the syntax used by a GUI client, the "Russian" one. Mine was a lurking duplicate cause by a bad "upsert" w/o the _id. It only showed up when adding a unique index.

Solution 12 - Mongodb

Solution and Example:

1- C:\MongoDB\Server\3.2\bin>mongo (do not issue command yet because you are not connected to any database yet, you are only connected to database server mongodb).

2- > show dbs analytics_database 0.000GB local 0.000GB test_database 0.000GB

3- > use test_database switched to db test_database

4- > db.Collection.remove({"_id": ObjectId("5694a3590f6d451c1500002e")}, 1); WriteResult({ "nRemoved" : 1 }) >

now you see WriteResult({ "nRemoved" : 1 }) is 1 not 0.

Done.

Solution 13 - Mongodb

There is new syntax no need to pass keyword object Id. Just pass like this it will work:

db.orders.remove({"_id":"62514d42f5aec503b2e0f2a9"})

Solution 14 - Mongodb

Execute the remove() method in this way

db.student.remove({ "_id" : ObjectId("627f593641bcd9e215bc949d")})
OUTPUT : WriteResult({ "nRemoved" : 1 })

enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTypo JohnsonView Question on Stackoverflow
Solution 1 - MongodbNic CottrellView Answer on Stackoverflow
Solution 2 - MongodbmjwrazorView Answer on Stackoverflow
Solution 3 - MongodbDmitriView Answer on Stackoverflow
Solution 4 - MongodbTypo JohnsonView Answer on Stackoverflow
Solution 5 - MongodbAnentropicView Answer on Stackoverflow
Solution 6 - MongodbKaroyView Answer on Stackoverflow
Solution 7 - MongodbSA KhanView Answer on Stackoverflow
Solution 8 - MongodbkamulaView Answer on Stackoverflow
Solution 9 - MongodbYuval MeshorerView Answer on Stackoverflow
Solution 10 - MongodbAsad SView Answer on Stackoverflow
Solution 11 - MongodbmckenzmView Answer on Stackoverflow
Solution 12 - MongodbDungView Answer on Stackoverflow
Solution 13 - MongodbAakash HandaView Answer on Stackoverflow
Solution 14 - MongodbPriyesh RanjanView Answer on Stackoverflow