MongoDB如何按计数字段删除文档
My collection has data with 10000+ records in the fields. It has two documents with 111 fields and 7 fields respectively.
I want to delete the document in collection with 7 fields. Please help suggest a query for this problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要删除包含 7 个字段的文档,请使用
db.collection.deleteOne()
方法与$expr
查询运算符,它允许您使用聚合运算符,例如$size
和$objectToArray
获取字段数量在文档中使用系统变量$$ROOT
作为根文档的参考。以下操作将删除具有
n
个字段的单个文档:To delete a document with 7 fields, use the
db.collection.deleteOne()
method with the$expr
query operator which allows you to use aggregate operators like$size
and$objectToArray
to get the number of fields in the document using the system variable$$ROOT
as reference to the root document.The following operation will delete a single document with
n
number of fields: