Mongo聚合管道:如何在后续阶段访问一个组阶段变量?

时间:2017-03-01 18:28:20

标签: mongodb aggregation-framework

我这里有两个系列:

> db.Unit.find({}).limit(1).pretty()
{
    "_id" : ObjectId("58b6878de1648d05903e1beb"),
    "number" : "07in15in4",
    "owner" : {
            "name" : "vacant"
    },
    "floor" : 15,
    "tower" : 4,
    "VisitorLogs" : [
            ObjectId("58b6878ee1648d05903e1d22"),
            ObjectId("58b6878ee1648d05903e236e"),
            ObjectId("58b6878ee1648d05903e23c9"),
            ObjectId("58b6878ee1648d05903e2454"),
            ObjectId("58b6878ee1648d05903e2915"),
            ObjectId("58b6878ee1648d05903e2aae"),
            ObjectId("58b6878ee1648d05903e2b93")
    ]

}

> db.VisitorLog.find({}).limit(1).pretty()
{ "_id" : ObjectId("58b6878fe1648d05903e2efa"), "purpose" : "WorkHere" }

Unit集合中的VisitorLogs引用第二个集合,即VisitorLog。

我需要找到每个目的访问次数最多的单位。

我在 mongo shell 中尝试了这个:

units=db.Unit
units.aggregate([
    {$match:{'VisitorLogs':{$gt:[]}}},
    {$unwind:'$VisitorLogs'},
    {$lookup:{
        from:"VisitorLog", 
        localField:"VisitorLogs", 
        foreignField:"_id", 
        as:"log"}
      },
    {$group:{_id:{number:"$number", purpose:"$log.purpose"}, count:{$sum:1}}},
    {$sort:{count:-1}},
    {$group:{_id:"$_id.purpose", unit:{$first:"$_id.number"}}},
    {$limit:10}
])  

我得到以下结果:

{ "_id" : [ "Business" ], "unit" : "05in12in2" }
{ "_id" : [ "WorkHere" ], "unit" : "09in04in2" }
{ "_id" : [ "Casual" ], "unit" : "10in05in2" }
{ "_id" : [ "JobInterview" ], "unit" : "05in14in2" }

这意味着,例如,单元号05in12in2的最大访问次数达到了目的" Business"

我现在希望得到" business"的数字 05in12in2的访问。 我认为它在"计数"第一小组阶段的变量。

我如何访问?我在第二阶段尝试{$group:{_id:"$_id.purpose", unit:{$first:"$_id.number"}, visits:"$count"}},,即在限制阶段之前,但我收到错误:

> units.aggregate([
... {$match:{'VisitorLogs':{$gt:[]}}},
... {$unwind:'$VisitorLogs'},
... {$lookup:{from:"VisitorLog", localField:"VisitorLogs", foreignField:"_id", as:"log"}},
... {$group:{_id:{number:"$number", purpose:"$log.purpose"}, count:{$sum:1}}},
... {$sort:{count:-1}},
... {$group:{_id:"$_id.purpose", unit:{$first:"$_id.number"}, visits:"$count"}},
... {$limit:10}
... ])
assert: command failed: {
    "ok" : 0,
    "errmsg" : "the group aggregate field 'visits' must be defined as an expression inside an object",
    "code" : 15951
} : aggregate failed
_getErrorWithCode@src/mongo/shell/utils.js:25:13
doassert@src/mongo/shell/assert.js:13:14
assert.commandWorked@src/mongo/shell/assert.js:287:5
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1312:5
@(shell):1:1

有人可以帮帮我吗?

2 个答案:

答案 0 :(得分:0)

您拥有此信息,但是您正在$group阶段切断它。只需将舞台更改为以下内容(注意最后一个参数):

{ $group : {
  _id : "$_id.purpose",
  unit : { $first : "$_id.number" },
  visits : { $first : "$count" }
}}

答案 1 :(得分:0)

以下输出我需要的内容:

units.aggregate([
    {$match:{'VisitorLogs':{$gt:[]}}},
    {$unwind:'$VisitorLogs'},
    {$lookup:{from:"VisitorLog", localField:"VisitorLogs", foreignField:"_id", as:"log"}},
    {$group:{_id:{number:"$number", purpose:"$log.purpose"}, count:{$sum:1}}},
    {$sort:{count:-1}},
    {$group:{_id:"$_id.purpose", number:{$first:"$_id.number"}, visits:{$first:"$count"}}},
    {$limit:10}
               ]) 
相关问题