我如何合并其他文档中的对象并将其放入MongoDB中的特定对象

时间:2020-04-23 19:59:01

标签: mongodb aggregation-framework

我有一个这样组织的收藏集:

{
        "_id" : ObjectId("5ea0842fb7c525173bd20e78"),
        "vehicleid" : 32040,
        "points" : {
                "direction" : 135,
                "latitude" : -3.744851,
                "longitude" : -38.545571,
                "odometer" : 55697826,
                "routecode" : 0,
                "speed" : 3,
                "deviceid" : 134680,
                "metrictimestamp" : ISODate("2018-02-01T02:59:59Z")
        }
}

我需要创建一个文档来存储具有相同“车辆标识”的文档中的所有“点”对象,例如:

 {
            "_id" : ObjectId("5ea0842fb7c525173bd20e78"),
            "vehicleid" : 32040,
            "points" :[ {
                    "direction" : 135,
                    "latitude" : -3.744851,
                    "longitude" : -38.545571,
                    "odometer" : 55697826,
                    "routecode" : 0,
                    "speed" : 3,
                    "deviceid" : 134680,
                    "metrictimestamp" : ISODate("2018-02-01T02:59:59Z")
            }, {    
                    "direction" : 135,
                    "latitude" : -3.235521,
                    "longitude" : -37.5122571,
                    "odometer" : 8456763,
                    "routecode" : 0,
                    "speed" : 4,
                    "deviceid" : 134680,
                    "metrictimestamp" : ISODate("2018-02-01T05:35:59Z") }
]
    }

任何想法?

2 个答案:

答案 0 :(得分:0)

您可以汇总管道

1-如果您需要保留原始文档_id,结果将与您在问题中提到的一样

db.collection.aggregate([
  {
    $match: {} // add your search filter here
  },
  {
    $group: { // this is to group all the points objects related to some vehicle 
      _id: "$vehicleid",
      originalId: {
        $first: "$_id" // to keep the _id of the original document, if you don't need it, you can omit this part
      },
      points: {
        $addToSet: "$points" // push the points objects without any duplication, if you just need to push them and duplication does not matter for you, user $push instead of $addToSet
      }
    }
  },
  {
    $project: { // this project step is used to get the original _id of the document, if you don't need it, you can omit all this project step
      _id: "$originalId",
      vehicleid: "$_id",
      points: 1
    }
  }
])

选中此Mongo Playground

请注意,这将仅获取第一个文档ID,如果许多文档具有相同的车辆ID,则您将获得的_id属于第一个文档,建议您选择选项2

2-如果您只需要带点的车辆ID,而没有原始文件_id

db.collection.aggregate([
  {
    $match: {}
  },
  {
    $group: {
      _id: "$vehicleid",
      points: {
        $addToSet: "$points"
      }
    }
  }
])

也对此进行检查Mongo Playground 2

答案 1 :(得分:0)

使用这两个 输入文档

{
        "_id" : 1,
        "vehicleid" : 32040,
        "points" : {
                "direction" : 135,
                "latitude" : -3.744851,
                "longitude" : -38.545571,
                "odometer" : 55697826,
                "routecode" : 0,
                "speed" : 3,
                "deviceid" : 134680,
                "metrictimestamp" : ISODate("2018-02-01T02:59:59Z")
        }
},
{
        "_id" : 2,
        "vehicleid" : 32040,
        "points" : {
                    "direction" : 135,
                    "latitude" : -3.235521,
                    "longitude" : -37.5122571,
                    "odometer" : 8456763,
                    "routecode" : 0,
                    "speed" : 4,
                    "deviceid" : 134680,
                    "metrictimestamp" : ISODate("2018-02-01T05:35:59Z")
        }
}

聚合查询

db.collection.aggregate( [
  {
      $group: {
          _id: { "vehicleid": "$vehicleid" },
          id: { $first: { _id: "$_id" } },
          points: { $push: "$points" }
      }
  },
 {
      $project: {
          _id: "$id._id",
          vehicleid: "$_id.vehicleid",
          points: "$points"
      }
  }
] ).pretty()

您将获得以下 结果

{
        "_id" : 1,
        "vehicleid" : 32040,
        "points" : [
                {
                        "direction" : 135,
                        "latitude" : -3.744851,
                        "longitude" : -38.545571,
                        "odometer" : 55697826,
                        "routecode" : 0,
                        "speed" : 3,
                        "deviceid" : 134680,
                        "metrictimestamp" : ISODate("2018-02-01T02:59:59Z")
                },
                {
                        "direction" : 135,
                        "latitude" : -3.235521,
                        "longitude" : -37.5122571,
                        "odometer" : 8456763,
                        "routecode" : 0,
                        "speed" : 4,
                        "deviceid" : 134680,
                        "metrictimestamp" : ISODate("2018-02-01T05:35:59Z")
                }
        ]
}