MongoDB 如何按计数字段删除文档
MongoDB how to delete document by count fields
我的集合中包含字段中超过 10000 条记录的数据。它有两个文档,分别有111个字段和7个字段。
我想删除集合中包含 7 个字段的文档。请帮助提出针对此问题的查询。
要删除包含 7 个字段的文档,请使用 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
作为对根文档的引用。
以下操作将删除具有 n
个字段的单个文档:
var n = 7;
db.collection.deleteOne({
$expr: {
$eq: [
{ $size: { $objectToArray: '$$ROOT' } },
n // n = 7
]
}
});
我的集合中包含字段中超过 10000 条记录的数据。它有两个文档,分别有111个字段和7个字段。
我想删除集合中包含 7 个字段的文档。请帮助提出针对此问题的查询。
要删除包含 7 个字段的文档,请使用 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
作为对根文档的引用。
以下操作将删除具有 n
个字段的单个文档:
var n = 7;
db.collection.deleteOne({
$expr: {
$eq: [
{ $size: { $objectToArray: '$$ROOT' } },
n // n = 7
]
}
});