mongodb - 查询通过从当前文档和下一个文档中减去一个值来获取一个值

标签 mongodb aggregation-framework subtraction

我有一个如下所示的 mongo 数据库集合,

{
    "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 数据库查询,它会给我当前的权重以及当前和下一条记录之间的权重差异。 下面的示例输出,

{
    "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']
              }

            }
        }
}
])

最佳答案

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

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" }}
])

关于mongodb - 查询通过从当前文档和下一个文档中减去一个值来获取一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52426914/

相关文章:

mongodb - 如何计算 MongoDB 中多个 GeoJSON 点之间的路线距离?

node.js - 检索与数组中最大值匹配的子文档

mysql - 我如何从 2 表中减去乘数行?数据库

mysql - UNION 查询中的部分加减法(减法)

mongodb - Mongo 用户定义函数和 Map Reduce

mongodb - Wildfly 8 中用于 CDI 的自定义 jndi 对象工厂

mongodb - 使用 S3 作为数据库与数据库(例如 MongoDB)

Node.JS + MongoDB 聚合框架平均值

MongoDB 文档嵌套文档过滤

python - Pandas 从同一数据框中减去 2 行