在 MongoDB 控制台中通过 _id 删除
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4932928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove by _id in MongoDB console
提问by Typo Johnson
In the MongoDB console how can I remove a record by id? Here's my collection :
在 MongoDB 控制台中,如何通过 id 删除记录?这是我的收藏:
[
{
"_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.orgif that makes any difference
如果这有什么不同,这在mongodb.org的浏览器外壳中
Thanks
谢谢
回答by Nic Cottrell
Very close. This will work:
很接近。这将起作用:
db.test_users.deleteOne( {"_id": ObjectId("4d512b45cc9374271b02ec4f")});
i.e. you don't need a newfor the ObjectId.
即你不需要一个新的 ObjectId。
Also, note that in some drivers/tools, remove()
is now deprecated and deleteOne
or deleteMany
should be used instead.
另外,请注意,在某些驱动程序/工具中,remove()
现在已弃用,deleteOne
或deleteMany
应改为使用。
回答by Typo Johnson
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 :
答案是 mongodb.org 上的 web 控制台/shell 的行为不同,而不是我预期的那样。在家中安装的版本可以完美运行,即;Web shell 上自动生成的 _id 保存如下:
"_id" : { "$oid" : "4d512b45cc9374271b02ec4f" },
The same document setup at home and the auto generated _id was saved like this :
在家中设置相同的文档和自动生成的 _id 保存如下:
"_id" : ObjectId("4d5192665777000000005490")
Queries worked against the latter without problem.
查询对后者没有问题。
回答by Dmitri
Well, the _id is an object in your example, so you just need to pass an object
好吧,_id 在你的例子中是一个对象,所以你只需要传递一个对象
'db.test_users.remove({"_id": { "$oid" : "4d513345cc9374271b02ec6c" }})'
This should work
这应该工作
Edit: Added trailing paren to ensure that it compiled.
编辑:添加尾随括号以确保它已编译。
回答by mjwrazor
If you would like to remove by a list of IDs this works great.
如果您想通过 ID 列表删除,这非常有效。
db.CollectionName.remove({
"_id": {
$in: [
ObjectId("0930292929292929292929"),
ObjectId("0920292929292929292929")
]
}
})
回答by Anentropic
Do you have multiple mongodb nodes in a replica set?
你在一个副本集中有多个 mongodb 节点吗?
I found (I am using via Robomongo gui mongo shell, I guess same applies in other cases) that the correct remove syntax, i.e.
我发现(我通过 Robomongo gui mongo shell 使用,我想在其他情况下同样适用)正确的删除语法,即
db.test_users.remove({"_id": ObjectId("4d512b45cc9374271b02ec4f")})
...does not work unless you are connected to the primarynode of the replica set.
...除非您连接到副本集的主节点,否则不起作用。
回答by SA Khan
first get the ObjectID function from the mongodb ObjectId = require(mongodb).ObjectID;
首先从mongodb中获取ObjectID函数 ObjectId = require(mongodb).ObjectID;
then you can call the _id with the delete function
然后你可以用删除函数调用_id
"_id" : ObjectId("4d5192665777000000005490")
"_id" : ObjectId("4d5192665777000000005490")
回答by Karoy
I've just bumped into this myself and this variation worked for me:
我自己刚刚遇到了这个问题,这个变化对我有用:
db.foo.remove({**_id**: new ObjectId("4f872685a64eed5a980ca536")})
回答by Yuval Meshorer
Even though this post is outdated, collection.remove is deprecated!collection.delete_one
should be used instead!
尽管这篇文章已经过时,但collection.remove 已被弃用!collection.delete_one
应该改用!
More information can be found here under #remove
更多信息可以在#remove下找到
回答by Asad S
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.
就是这样。
回答by Dung
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).
1- C:\MongoDB\Server\3.2\bin>mongo(不要发出命令,因为你还没有连接到任何数据库,你只连接到数据库服务器mongodb)。
2-
2-
show dbs analytics_database 0.000GB local 0.000GB test_database 0.000GB
显示 dbs analytics_database 0.000GB 本地 0.000GB test_database 0.000GB
3-
3-
use test_database switched to db test_database
使用 test_database 切换到 db test_database
4-
4-
db.Collection.remove({"_id": ObjectId("5694a3590f6d451c1500002e")}, 1); WriteResult({ "nRemoved" : 1 })
db.Collection.remove({"_id": ObjectId("5694a3590f6d451c1500002e")}, 1); WriteResult({ "nRemoved" : 1 })
now you see WriteResult({ "nRemoved" : 1 }) is 1 not 0.
现在您看到 WriteResult({ "nRemoved" : 1 }) 是 1 而不是 0。
Done.
完毕。