具有自身属性比较的mongodb查询
我有这样的文件具有自身属性比较的mongodb查询,mongodb,mongodb-query,aggregation-framework,Mongodb,Mongodb Query,Aggregation Framework,我有这样的文件 { "_id": ObjectId("524a498ee4b018b89437f88a"), "counter": { "0": { "date": "2013.9", "counter": NumberInt(1425) }, "1": { "date": "2013.10", "counter": NumberInt(1425) } }, "profile"
{
"_id": ObjectId("524a498ee4b018b89437f88a"),
"counter": {
"0": {
"date": "2013.9",
"counter": NumberInt(1425)
},
"1": {
"date": "2013.10",
"counter": NumberInt(1425)
}
},
"profile": ObjectId("510576242b5e30877c654aff")
}
我想搜索那些,其中counter.0.counter不等于counter.1.counter
尝试
但它表示这不是一个有效的json查询:/
帮忙 两件事
除非求助于JavaScript或使用聚合框架,否则实际上无法进行这样的比较。使用聚合的表单是更好的选择:
db.collection.aggregate([
{“$project”:{
"柜台":一,,
“匹配”:{“$eq”:[
“$counter.0.counter”,
“$counter.1.计数器”
]}
}},
{“$match”:{“matched”:true}
])
或者,如果JavaScript使用不当:
db.collection.find({
“$where”:函数(){
返回this.counter.0.counter==this.counter.1.counter;
}
})
因此,这些就是可以做到这一点的方法
JavaScript$where
操作符的主要问题是:
- 调用JavaScript解释器来计算每个结果文档,而不是本机代码
- 根据需要删除使用索引查找结果的任何机会。通过其他方法,您实际上可以使用带有单独“匹配”条件的索引。但这个操作员排除了这个机会
db.counter.find({"profile":ObjectId("510576242b5e30877c654aff"),"counter.0.counter":{$ne:"counter.1.counter"} });