在MongoDB控制台中按_id删除 [英] Remove by _id in MongoDB console

查看:199
本文介绍了在MongoDB控制台中按_id删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MongoDB控制台中,如何按ID删除记录?这是我的收藏:

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":"4d513345cc9374271b02ec6c"}});
db.test_users.remove( {"_id":"4d512b45cc9374271b02ec4f"});
db.test_users.remove( {"_id":new ObjectId("4d512b45cc9374271b02ec4f")});

按名称删除有效:

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

如果有什么不同,它位于 mongodb.org 的浏览器外壳中

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

谢谢

推荐答案

非常接近.这将起作用:

Very close. This will work:

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

即您不需要新建作为ObjectId.

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

此外,请注意,在某些驱动程序/工具中,现在不建议使用remove(),而应改用deleteOnedeleteMany.

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

这篇关于在MongoDB控制台中按_id删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆