Home > Net >  group values using moongoose
group values using moongoose

Time:06-09

{
    "_id":"62a1a47ee3a37826bb647518",
    "lid":"629df665563f56ec1a638ea0",
    "date": "7",
    "time": "1654583101416",
    "location": {
        "type": "Point",
        "coordinates": [75.26438,11.29343]
    }
}
{
    "_id":"62a1a47ee3a37826bb647539",
    "lid":"629df665563f56ec1a638ea0",
    "date": "7",
    "time": "1654583101416",
    "location": {
        "type": "Point",
        "coordinates": [75.26438,11.29343]
    }
}

I want to group this document by same coordinates values(if two documents have same coordinates values it returned as one document)

CodePudding user response:

It depends on what you want to do with other fields. I added grouping by coordinates with adding other fields to sets. Here is the Mongo playground

db.collection.aggregate([
  {
    "$group": {
      "_id": "$location.coordinates",
      lid: {
        $addToSet: "$lid"
      },
      date: {
        $addToSet: "$date"
      },
      time: {
        $addToSet: "$time"
      },
      location_type: {
        $addToSet: "$location.type"
      }
    }
  }
])

  • Related