如何 return 聚合整个文档

How to return whole document from aggregation

我目前正在使用 MonGODB v2.6.4,没有其他原因,Robomongo 尚不支持 MongoDB v3.0。

使用以下示例文档,如果它不是我正在尝试做的事情的缩减示例,我不会这样构建它们。

db.visitors.insert({
    "visits" : [{"building" : "building01", "lastVisit" : new ISODate("2015-02-01 17:05:00")},
                {"building" : "building02", "lastVisit" : new ISODate("2015-02-04 04:25:00")},
                {"building" : "building03", "lastVisit" : new ISODate("2015-02-07 15:45:00")},
                {"building" : "building04", "lastVisit" : new ISODate("2015-02-10 15:45:00")}
               ],

 "firstName" : "John",
 "lastName" : "Smith",
 "gender" : "male",
 "accountNumber" : "123456789",
 });

db.visitors.insert({
    "visits" : [{"building" : "building01", "lastVisit" : new ISODate("2015-02-02 17:05:00")},
                {"building" : "building02", "lastVisit" : new ISODate("2015-02-05 04:25:00")},
                {"building" : "building03", "lastVisit" : new ISODate("2015-02-08 15:45:00")},
                {"building" : "building04", "lastVisit" : new ISODate("2015-02-11 15:45:00")}
               ],

 "firstName" : "Jane",
 "lastName" : "Smith",
 "gender" : "female",
 "accountNumber" : "987654321",
 });

db.visitors.insert({
    "visits" : [{"building" : "building01", "lastVisit" : new ISODate("2015-02-03 17:05:00")},
                {"building" : "building02", "lastVisit" : new ISODate("2015-02-06 04:25:00")},
                {"building" : "building03", "lastVisit" : new ISODate("2015-02-09 15:45:00")},
                {"building" : "building04", "lastVisit" : new ISODate("2015-02-12 15:45:00")}
               ],

 "firstName" : "James",
 "lastName" : "Smith",
 "gender" : "male",
 "accountNumber" : "123056780",
 });

我想找到特定建筑物的最后一位访客,并且需要该访客的整个文档 returning。

我已经制定了这个聚合查询,几乎可以满足我的要求:

db.visitors.aggregate([
  {$match: {"visits.building": "building02"}},
  {$unwind: "$visits"},
  {$project: {"visitorId": "$_id", "building": "$visits.building", "lastVisit": "$visits.lastVisit"}},
  {$sort: {"lastVisit": 1}},
  {$group: {"_id": "$building", "visitorId": {$last: "$visitorId"}, "lastVisit": {$last: "$lastVisit"}}},
  {$match: {"_id": "building02"}},
  {$limit: 1}
])

和return这个结构:

{
    "result" : [ 
        {
            "_id" : "building02",
            "visitorId" : ObjectId("55098990ca5b44f2858f4cb5"),
            "lastVisit" : ISODate("2015-02-12T15:45:00.000000:00")
        }
    ],
    "ok" : 1.0000000000000000
}

如何将聚合查询修改为 return 整个访问者文档,而不仅仅是 visitorId?我已经尝试过(使用 $$ROOT$$CURRENT 失败)。

如果无法 return 访问整个文档,那么我该如何在 return 访问的结果结构中进行查找,以便我可以通过 ID 检索它?我一直在尝试这个主题的变体:

var result = db.visitors.aggregate([
  {$match: {"visits.building": "building02"}},
  {$unwind: "$visits"},
  {$project: {"visitorId": "$_id", "building": "$visits.building",  "lastVisit": "$visits.lastVisit"}},
  {$sort: {"lastVisit": 1}},
  {$group: {"_id": "$building", "visitorId": {$last: "$visitorId"}, "lastVisit": {$last: "$lastVisit"}}},
  {$match: {"_id": "building02"}},
  {$limit: 1}
])

db.individuals.find({_id: {$eq: {result.visitorId: {$slice: [0, 1]}}}})

我更愿意在一个查询中完成所有事情,但如果我不能,那我就不能。

您不需要 $group,因此无需担心 $$ROOT 或努力取回所有字段。您将取回原始文档的(副本),visits 数组中只剩下最近一次访问。

> db.visitors.aggregate([ 
    { "$match" : { "visits.building" : "building02" } },
    { "$unwind" : "$visits" }, 
    { "$match" : { "visits.building" : "building02" } },
    { "$sort" : { "visits.lastVisit" : -1 } }, { "$limit" : 1 }
])
{
    "_id" : ObjectId("5509e963b8b4702f49ffcee6"),
    "visits" : {
        "building" : "building02",
        "lastVisit" : ISODate("2015-02-06T04:25:00Z")
    },
    "firstName" : "James",
    "lastName" : "Smith",
    "gender" : "male",
    "accountNumber" : "123056780"
}

如果你想要整个 visits 数组,那么我只是通过管道投影 _id 并在管道 returns 之后用它进行查找,以获得整个文档。