如何从聚合返回整个文档

时间:2015-03-18 14:48:15

标签: mongodb mongodb-query

我目前正在使用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",
 });

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

我已经解决了这个几乎完成我想要的聚合查询:

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

并返回此结构:

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

如何修改聚合查询以返回整个访问者文档而不仅仅是visitorId?我尝试过(使用$$ROOT$$CURRENT失败了。)

如果无法返回整个文档,那么我如何对返回的结果结构进行查找,以便我可以通过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]}}}})

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

1 个答案:

答案 0 :(得分:2)

您不需要$ 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并在管道返回后用它进行查找,以获得整个文档。