Query to get a value by subtracting a value from current and next document

为君一笑 提交于 2019-12-10 21:26:37

问题


I have a mongo db collection like below,

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

I need a mongo db query that will give me the current weight and the weight difference between the current and next record. Example output below,

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

I was not able to get the weight from next document to subtract the weight from current document. Thanks in advance for your help

My try for the above problem,

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

            }
        }
}
])

回答1:


You can try below aggregation but I will not recommend you to use this with the large data set.

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


来源:https://stackoverflow.com/questions/52426914/query-to-get-a-value-by-subtracting-a-value-from-current-and-next-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!