MongoDB如何按计数字段删除文档

发布于 2025-01-14 04:56:54 字数 233 浏览 0 评论 0原文

输入图片此处描述

我的收藏包含字段中包含 10000 多条记录的数据。它有两个文档,分别有111个字段和7个字段。

我想删除集合中包含 7 个字段的文档。请帮助建议对此问题的查询。

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

み零 2025-01-21 04:56:55

要删除包含 7 个字段的文档,请使用 db.collection.deleteOne() 方法与 $expr 查询运算符,它允许您使用聚合运算符,例如 $size$objectToArray 获取字段数量在文档中使用系统变量 $$ROOT作为根文档的参考。

以下操作将删除具有 n 个字段的单个文档:

var n = 7;
db.collection.deleteOne({
    $expr: {
        $eq: [
            { $size: { $objectToArray: '$ROOT' } },
            n // n = 7
        ]
    }    
});

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:

var n = 7;
db.collection.deleteOne({
    $expr: {
        $eq: [
            { $size: { $objectToArray: '$ROOT' } },
            n // n = 7
        ]
    }    
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文