【问题标题】:MongoDB aggregation framework $subtractMongoDB 聚合框架 $subtract
【发布时间】:2013-02-07 06:07:39
【问题描述】:

我想用mongodb实现mysql“select a-b from table”这样的简单查询,但是聚合框架查询结果不对。

数据:

{ "_id" : ObjectId("511223348a88785127a0d13f"), "a" : 1, "b" : 1, "name" : "xxxxx0" }
{ "_id" : ObjectId("511223348a88785127a0d13f"), "a" : 2, "b" : 2, "name" : "xxxxx1" }

mongodb 命令:

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
        },
]

【问题讨论】:

    标签: mongodb aggregation-framework


    【解决方案1】:

    我无法复制你的行为:

    > 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 版本?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-20
      • 2012-09-24
      • 1970-01-01
      • 2021-06-15
      • 2015-06-28
      • 2013-05-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多