Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MongoDB mgo聚合最早创建日期和最晚最后修改日期_Mongodb_Date_Go_Aggregation_Mgo - Fatal编程技术网

MongoDB mgo聚合最早创建日期和最晚最后修改日期

MongoDB mgo聚合最早创建日期和最晚最后修改日期,mongodb,date,go,aggregation,mgo,Mongodb,Date,Go,Aggregation,Mgo,我正在尝试检索以下数据中每个对象名称的最早创建日期和最晚最后修改日期 { "_id" : ObjectId("5a510666b2e543371cff44ef"), "object_name" : "A", "username" : "user1", "created_at" : ISODate("2018-01-06T17:24:54.026Z"), "last_modified" : ISODate("2018-01-06T17:24:54.026Z") } { "_id" : Object

我正在尝试检索以下数据中每个对象名称的最早创建日期和最晚最后修改日期

{ "_id" : ObjectId("5a510666b2e543371cff44ef"), "object_name" : "A", "username" : "user1", "created_at" : ISODate("2018-01-06T17:24:54.026Z"), "last_modified" : ISODate("2018-01-06T17:24:54.026Z") }
{ "_id" : ObjectId("5a5106e7b2e543371cff4515"), "object_name" : "A", "username" : "user1", "created_at" : ISODate("2018-01-06T17:27:03.262Z"), "last_modified" : ISODate("2018-01-06T17:27:03.262Z") }
{ "_id" : ObjectId("5a510933b2e543371cff45be"), "object_name" : "B", "username" : "user1", "created_at" : ISODate("2018-01-06T17:36:51.300Z"), "last_modified" : ISODate("2018-01-06T17:36:51.300Z") }
{ "_id" : ObjectId("5a510939b2e543371cff45c5"), "object_name" : "C", "username" : "user2", "created_at" : ISODate("2018-01-06T17:36:57.058Z"), "last_modified" : ISODate("2018-01-06T17:36:57.058Z") }
我可以通过以下管道将不同的object_名称与每个用户名分组

pipeline := []bson.M{
    {"$group": bson.M{"_id": "$username", "objects": bson.M{"$addToSet": "$object_name"}}},
}
但是,我还想为每个对象添加最早的创建日期和最晚的最后修改日期

这是我想要的输出:

[
    {
        "_id": "user1",
        "objects": [
            {
                "object_name": "A",
                "created_at": "2018-01-06T17:24:54.026Z",
                "last_modified": "2018-01-06T17:27:03.262Z"
            },
            {
                "object_name": "B",
                "created_at": "2018-01-06T17:36:51.300Z",
                "last_modified": "2018-01-06T17:36:51.300Z"
            }
        ]
    },
    {
        "_id": "user2",
        "objects": [
            {
                "object_name": "C",
                "created_at": "2018-01-06T17:36:57.058Z",
                "last_modified": "2018-01-06T17:36:57.058Z"
            }
        ]
    }
]

我认为它与$sort和$first或$last有关,但我不知道如何将它们组合在一起。

您可以尝试下面的聚合查询

db.colname.aggregate([
{"$group":{
  "_id":{
    "username":"$username",
    "object_name":"$object_name"
  },
  "created_at":{"$min":"$created_at"},
  "last_modified":{"$max":"$last_modified"}
}},
{"$group":{
  "_id":"$_id.username",
  "objects":{
    "$push":{
      "object_name":"$_id.object_name",
      "created_at":"$created_at",
      "last_modified":"$last_modified"
    }
  }
}}])

对不起,迟了答复。这正是我想要的。谢谢。