在MongoDB中仅删除一个文档 [英] Remove only one document in MongoDB
本文介绍了在MongoDB中仅删除一个文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我打电话时
db.collection.remove({'condition':'some condition'});
这一行将删除所有匹配的条件文档.
This one line will delete all the matching condition documents.
如果我只想删除任何一个或第n个匹配条件,该怎么办?
What If I want to remove only one of any or n-th matching condition?
推荐答案
您需要为此执行两个单独的查询
You need to perform two separate queries for this
-
仅从匹配的过滤器中提取一项
take only one item from the matching filters
var item = db.collection.findOne({'condition':'some condition'})
并使用ID删除项目
and delete the item by using the id
db.collection.remove({_id: item._id});
这篇关于在MongoDB中仅删除一个文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文