Typo Johnson
Typo Johnson

Reputation: 6044

Remove by _id in MongoDB console

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.

Upvotes: 178

Views: 274332

Answers (15)

Nic Cottrell
Nic Cottrell

Reputation: 9695

Very close. This will work:

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

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

This is equivalent (in Extended JSON) to:

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

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

Upvotes: 335

Dmitri
Dmitri

Reputation: 36300

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

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

Upvotes: 20

Asad S
Asad S

Reputation: 2116

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 }

Upvotes: 4

Dung
Dung

Reputation: 20615

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.

Upvotes: 1

Aakash Handa
Aakash Handa

Reputation: 1307

There is new syntax. No need to pass keyword object Id. Just pass like this:

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

Upvotes: 0

Rudresh Oza
Rudresh Oza

Reputation: 83

There is a new way to delete the object key(ObjectId) i.e. store in _id column

  • const ObjectId = require("mongodb").ObjectId
  • db.collection.deleteOne({_id:**new ObjectId(id)**}) // without double quotes

Upvotes: 0

Priyesh Ranjan
Priyesh Ranjan

Reputation: 111

Execute the remove() method in this way

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

enter image description here

Upvotes: 1

mckenzm
mckenzm

Reputation: 1840

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.

Upvotes: 2

kamula
kamula

Reputation: 317

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

Upvotes: 5

Yuval Meshorer
Yuval Meshorer

Reputation: 166

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

Upvotes: 3

Anentropic
Anentropic

Reputation: 33923

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.

Upvotes: 8

SA Khan
SA Khan

Reputation: 87

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

Upvotes: 8

mjwrazor
mjwrazor

Reputation: 1994

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

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

Upvotes: 25

Karoy
Karoy

Reputation: 193

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

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

Upvotes: 7

Typo Johnson
Typo Johnson

Reputation: 6044

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.

Upvotes: 17

Related Questions