[英]How can I convert an array to an object in the $group stage of an aggregation pipeline?
[英]Mongo aggregation pipeline : How can I access one group stage variable in subsequent stage?
我在这里有两个收藏:
> 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的访问量最高,其目的是“业务”
我现在想的是05in12in2有“业务”的访问次数 。 我认为它是在小组赛第一阶段的“ count”变量中。
我该如何访问? 我在倒数第二个阶段(即在限制之前)尝试了{$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
有人可以帮帮我吗 ?
您具有此信息,但是您要在$group
阶段将其切断。 只需将阶段更改为以下内容(请注意最后一个参数):
{ $group : {
_id : "$_id.purpose",
unit : { $first : "$_id.number" },
visits : { $first : "$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", number:{$first:"$_id.number"}, visits:{$first:"$count"}}},
{$limit:10}
])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.