【问题标题】:Query to get a value by subtracting a value from current and next document通过从当前文档和下一个文档中减去一个值来查询以获取一个值
【发布时间】:2018-09-20 13:59:23
【问题描述】:

我有一个如下所示的 mongo db 集合,

{
    "id": ObjectId("132456"),
    reading :[
        {
            "weight" : {
            "measurement" : 82.0,
            "unit" : "kg"
               }
        }
    ],
    "date" : ISODate("2018-09-12T11:45:08.174Z")
},
{
    "id": ObjectId("132457"),
    reading :[
        {
            "weight" : {
            "measurement" : 80.0,
            "unit" : "kg"
               }
        }
    ],
    "date" : ISODate("2018-09-12T10:45:08.174Z")
},
{
    "id": ObjectId("132458"),
    reading :[
        {
            "weight" : {
            "measurement" : 85.0,
            "unit" : "kg"
               }
        }
    ],
    "date" : ISODate("2018-09-11T09:45:08.174Z")
}

我需要一个 mongo db 查询,它会给我当前的权重以及当前和下一条记录之间的权重差异。 下面的示例输出,

{
    "id": ObjectId("132456"),
    "currentWeight": 75.0,
    "weightDifference": 2.0,
    "date" : ISODate("2018-09-12T11:45:08.174Z")
},
{
    "id": ObjectId("132457"),
    "currentWeight": 80.0,
    "weightDifference": -5.0,
    "date" : ISODate("2018-09-12T10:45:08.174Z")
}

我无法从下一个文档中获取权重以从当前文档中减去权重。 提前感谢您的帮助

我对上述问题的尝试,

db.measurementCollection.aggregate([
{
    $match : { "date" : { $gte : new ISODate("2018-09-01T00:00:00.000Z") , $lte : new ISODate("2018-09-12T23:59:59.000Z")  }  }
},
{ 
    $project : { "date" : 1 , 
    "currentWeight" : {$arrayElemAt: [ "$reading.weight.measurement", 0 ]}
},
{ $sort: {"date":-1} },
{ 
    $addFields : {
        "weigtDifference" : 
            {
                {
                    $limit: 2
                },
                {
                    $group: {
                      _id: null,
                      'count1': {$first: '$currentWeight'},
                      'count2': {$last: '$currentWeight'}
                    }
                },
              {
                $subtract: ['$count1', '$count2']
              }

            }
        }
}
])

【问题讨论】:

  • 您是否可以向我们展示您迄今为止的尝试,以便我们从那里为您提供帮助?
  • 感谢您的回复@chridam。我已经尝试更新了描述。

标签: mongodb aggregation-framework subtraction


【解决方案1】:

您可以尝试以下聚合,但我不建议您将其用于大型数据集。

db.collection.aggregate([
  { "$match": {
    "date" : {
      "$gte": new ISODate("2018-09-01T00:00:00.000Z"),
      "$lte": new ISODate("2018-09-12T23:59:59.000Z")
    }
  }},
  { "$unwind": "$reading" },
  { "$sort": { "date": -1 }},
  { "$group": { "_id": null, "data": { "$push": "$$ROOT" }}},
  { "$project": {
      "data": {
        "$filter": {
          "input": {
            "$map": {
              "input": { "$range": [0, { "$size": "$data" }] },
              "as": "tt",
              "in": {
                "$let": {
                  "vars": {
                    "first": { "$arrayElemAt": ["$data", "$$tt"] },
                    "second": { "$arrayElemAt": ["$data", { "$add": ["$$tt", 1] }] }
                  },
                  "in": {
                    "currentWeight": "$$first.reading.weight.measurement",
                    "weightDifference": { "$subtract": ["$$second.reading.weight.measurement", "$$first.reading.weight.measurement"] },
                    "_id": "$$first._id",
                    "date": "$$first.date"
                  }
                }
              }
            }
          },
          "cond": { "$ne": ["$$this.weightDifference", null] }
        }
      }
    }
  },
  { "$unwind": "$data" },
  { "$replaceRoot": { "newRoot": "$data" }}
])

【讨论】:

  • 您在$group 之前缺少$sort。
  • 成功了。感谢@Anthony Winzlet 和 dnickless 的支持。
  • @TechnicianS 请记住我的这条评论但我不建议您将其用于大型数据集。您的$match 条件应始终为短距离。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-17
  • 2021-08-30
  • 1970-01-01
  • 1970-01-01
  • 2015-01-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多