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@email.com"
}
]
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":"4d513345cc9374271b02ec开发者_C百科6c"}});
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
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.
If you would like to remove by a list of IDs this works great.
db.CollectionName.remove({
"_id": {
$in: [
ObjectId("0930292929292929292929"),
ObjectId("0920292929292929292929")
]
}
})
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.
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.
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")
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.
I've just bumped into this myself and this variation worked for me:
db.foo.remove({**_id**: new ObjectId("4f872685a64eed5a980ca536")})
db.collection("collection_name").deleteOne({_id:ObjectId("4d513345cc9374271b02ec6c")})
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
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.
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 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.
There is new syntax no need to pass keyword object Id. Just pass like this it will work:
db.orders.remove({"_id":"62514d42f5aec503b2e0f2a9"})
Execute the remove() method in this way
db.student.remove({ "_id" : ObjectId("627f593641bcd9e215bc949d")})
OUTPUT : WriteResult({ "nRemoved" : 1 })
精彩评论