本文介绍了MongoDB聚合框架$ subtract的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用mongodb来实现简单的查询,例如mysql从表中选择a-b",但是聚合框架的查询结果不正确.
I'm want use mongodb to achieve simple query like mysql "select a-b from table", but aggregation framework query result is not right.
数据:
{ "_id" : ObjectId("511223348a88785127a0d13f"), "a" : 1, "b" : 1, "name" : "xxxxx0" } { "_id" : ObjectId("511223348a88785127a0d13f"), "a" : 2, "b" : 2, "name" : "xxxxx1" }mongodb cmd:
mongodb cmd:
db.site.aggregate([ { $match: { "a" : {$exists:true}, "b" : {$exists:true}, } }, { $project: { _id : 0,name : 1, r1: {$subtract:["$a", "$b"]} } }, { $limit: 100 }, ]); "result" : [ { "name" : "xxxx1", "r1" : -1 }, { "name" : "xxxx0", "r1" : -2 }, ]推荐答案
我无法复制您的行为:
> db.tg.find() { "_id" : ObjectId("511223348a88785127a0d13f"), "a" : 1, "b" : 1, "name" : "xxxxx0" } > db.tg.aggregate([{ $match: { "a" : {$exists:true}, "b" : {$exists:true} } }, { $project: { _id : 0,name : 1, r1: {$subtract:["$a", "$b"]} }}, { $limit: 100 }]) { "result" : [ { "name" : "xxxxx0", "r1" : 0 } ], "ok" : 1 }您能给我们更多信息,例如您的MongoDB版本吗?
Can you give us a little more info like your MongoDB version?
更多推荐
MongoDB聚合框架$ subtract
发布评论