MongoDB:聚合框架:字段之间的$ match [英] MongoDB : aggregation framework : $match between fields
本文介绍了MongoDB:聚合框架:字段之间的$ match的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含两个文档的测试集合:
I have a test collection with two documents :
> db.test.find().pretty()
{ "_id" : ObjectId("510114b46c1a3a0f6e5dd7aa"), "a" : 1, "b" : 2 }
{ "_id" : ObjectId("510114c86c1a3a0f6e5dd7ab"), "a" : 3, "b" : 1 }
使用聚合框架,我只想获取a大于b的文档. $ gt仅获取参数中的值,而不获取字段中的值...
With aggregation framework, I want to get only the documents where a is greater than b. $gt get only values in argument not fields...
> db.test.aggregate([{"$match":{"$a":{"$gt":"$b"}}}])
{ "result" : [ ], "ok" : 1 } /* don't work*/
您有什么想法吗?
预先感谢
最诚挚的问候
推荐答案
嗯,我没有做太多测试,我会说你可以使用$cmp
来实现这一目的:
Hmm without much testing on my end I will say you can use $cmp
for this:
http://docs.mongodb.org/manual/reference/aggregation /cmp/#_ S_cmp
db.test.aggregate([
{$project: {
// All your other fields here
cmp_value: {$cmp: ['$a', '$b']}
}},
{$match: {cmp_value: {$gt: 0}}}
])
也许有更好的方法,但是我附近没有MongoDB安装可以测试.
There might be a better way but I haven't got a MongoDB installation near me to test.
这篇关于MongoDB:聚合框架:字段之间的$ match的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文