我在MongoDB中有以下声明:
db.getCollection('Forms').find({"Id": { $nin: db.Forms.find({"Status": "DELETE" }, {_id:0, Id:1}).map(function (fr) { return fr.Id;} ) }})
Forms集合是一个仅附加的集合,可以写入ADD,UPDATE和DELETE状态记录以及这个"选择"确实是从没有DELETE记录的集合中返回所有表单。
问题是:如何使用MongoDB驱动程序编写过滤器?我尝试了以下内容,它返回一个空列表:
new BsonDocument("Id", "{ $nin: db.Forms.find({\"Status\": \"DELETE\" }, {_id:0, Id:1}).map(function (fr) { return fr.Id;} ) }");
下面是集合中某些行的示例,我已经排除了大部分字段,只是在这里选择了相关字段:
/* 1 */{"_id" : ObjectId("55caeb15b33c2d1ff84a618c"),"Id" : "55cae279b33c2d0c2831340c","Status" : "DELETE"}
/* 2 */{"_id" : ObjectId("55cae279b33c2d0c2831340c"),"Id" : "55cae279b33c2d0c2831340c","Status" : "ADD"}
/* 3 */{"_id" : ObjectId("55cae263b33c2d0c2831340b"),"Id" : "55cae21eb33c2d1c187fb2b8","Status" : "DELETE"}
/* 4 */{"_id" : ObjectId("55cae21eb33c2d1c187fb2b8"),"Id" : "55cae21eb33c2d1c187fb2b8","Status" : "ADD"}
/* 5 */{"_id" : ObjectId("55cae200b33c2d1c187fb2b6"),"Id" : "55cae177b33c2d1c187fb2b1","Status" : "DELETE"}
/* 6 */{"_id" : ObjectId("55cae192b33c2d1c187fb2b2"),"Id" : "55cae177b33c2d1c187fb2b1","Status" : "UPDATE"}
/* 7 */{"_id" : ObjectId("55cae177b33c2d1c187fb2b1"),"Id" : "55cae177b33c2d1c187fb2b1","Status" : "ADD"}
/* 8 */{"_id" : ObjectId("55cad0bcb33c2d1040d49d25"),"Id" : "55c9ec2bb33c2d2d04126cf7","Status" : "DELETE"}
/* 9 */{"_id" : ObjectId("55c9f5f0b33c2c27947ef05b"),"Id" : "55c9ec2bb33c2d2d04126cf7","Status" : "UPDATE"}
/* 10 */{"_id" : ObjectId("55c9f5c5b33c2c27947ef05a"),"Id" : "55c9ec2bb33c2d2d04126cf7","Status" : "UPDATE"}
/* 19 */{"_id" : ObjectId("55c9ec2bb33c2d2d04126cf7"),"Id" : "55c9ec2bb33c2d2d04126cf7","Status" : "ADD"}
...
/* 44 */{"_id" : ObjectId("55c32785b33c2c206014caf0"),"Id" : "55c08ea1b33c2d24f0aaf697","Status" : "UPDATE"}
/* 45 */{"_id" : ObjectId("55c31762b33c2c235cbef0f9"),"Id" : "55c08ea1b33c2d24f0aaf697","Status" : "UPDATE"}
/* 46 */{"_id" : ObjectId("55c31304b33c2c235cbef0e9"),"Id" : "55c08ea1b33c2d24f0aaf697","Status" : "UPDATE"}
/* 47 */{"_id" : ObjectId("55c2071fb33c2d0d1ca701fe"),"Id" : "55c08ea1b33c2d24f0aaf697","Status" : "UPDATE"}
/* 48 */{"_id" : ObjectId("55c1ee9db33c2d2090685c29"),"Id" : "55c08ea1b33c2d24f0aaf697","Status" : "UPDATE"}
/* 49 */{"_id" : ObjectId("55c1e267b33c2d2090685bff"),"Id" : "55c08ea1b33c2d24f0aaf697","Status" : "UPDATE"}
/* 50 */{"_id" : ObjectId("55c1b2eeb33c2c1f0cb151c2"),"Id" : "55c08ea1b33c2d24f0aaf697","Status" : "UPDATE"}
因此,select需要排除所有具有" DELETE"的表单(而不是行)。条目 - 即,只要有表格的DELETE记录,就必须从列表中排除。
澄清一下:_id字段是每行的唯一ID,而Id是我在选择中使用的表单的ID。
编辑虽然@ BlakesSeven建议的重复回答向我解释说我无法调用函数" inline"就像我试图做的那样,问题仍然存在:如何使用MongoDB C#驱动程序将这行代码编写为MapReduce :
db.getCollection('Forms').find({"Id": { $nin: db.Forms.find({"Status": "DELETE" }, {_id:0, Id:1}).map(function (fr) { return fr.Id;} ) }})
答案 0 :(得分:0)
Mongo shell是一个非常先进的MongoDB客户端 在这种情况下,它会为您执行2个查询,而不是单个查询 作为证明,您可以探索执行计划,它们将是相同的(查询的格式是为了便于阅读):
db.getCollection('Forms')
.find({"Id": { $nin: db.Forms.find({"Status": "DELETE" }, {_id:0, Id:1}).map(function (fr) { return fr.Id;} )}})
.explain()
并且
var excludedIds = db.Forms.find({"Status": "DELETE" }, {_id:0, Id:1}).map(function (fr) { return fr.Id;} )
db.getCollection('Forms')
.find({"Id": { $nin: excludedIds }})
.explain()
因此,您需要在客户端执行相同操作:首先查询excludedIds
,然后再次创建基于excludedIds
的过滤器并再次查询Forms
集合。