当我这样做时$lookup,在我的情况下foreignField:"_id",我在数组中得到找到的元素.下面是从后输出一个文档$lookup已经做了检索fromUser,并toUser从users集合:
{
_id : { from : 57b8da368e4a6e1f0043cb3d, to : 57c381af7008e51f009d92df },
fromUser : [
{
_id : 57b8da368e4a6e1f0043cb3d,
userName: "A"
}
],
toUser : [
{
_id : 57c381af7008e51f009d92df,
userName: "B"
}
]
}
Run Code Online (Sandbox Code Playgroud)
你可以注意到fromUser并且toUser是数组.如何投射fromUser,toUser而不是数组,他们只包含用户的userName,如下所示:
{
_id : { from : 57b8da368e4a6e1f0043cb3d, to : 57c381af7008e51f009d92df },
fromUser: "A",
toUser: "B"
}
Run Code Online (Sandbox Code Playgroud) 我有一个包含如下文档的集合:
{
"_id" : ObjectId("55b377cb66b393427367c3e2"),
"comment" : "This is a comment",
"url_key" : "55b377cb66b393427367c3df", //This is an ObjectId from another record in a different collection
}
Run Code Online (Sandbox Code Playgroud)
我需要在此集合中找到包含注释和url_key重复值的记录。
我可以轻松地生成(使用汇总)相同,单个键(例如:注释)的重复记录,但是我不知道如何对多个关键字进行分组/汇总。
这是我当前的聚合管道:
db.comments.aggregate([ { $group: { _id: { comment: "$comment" }, uniqueIds: { $addToSet: "$_id" }, count: { $sum: 1 } } }, { $match: { count: { $gte: 2 } } }, { $sort: { count : -1} }, {$limit 10 } ]);
Run Code Online (Sandbox Code Playgroud) mongodb mongodb-query aggregation-framework mongodb-aggregation
有聚合管道:
db.getCollection('yourCollection').aggregate(
{
$unwind: {
path: "$dates",
includeArrayIndex: "idx"
}
},
{
$project: {
_id: 0,
dates: 1,
numbers: { $arrayElemAt: ["$numbers", "$idx"] },
goals: { $arrayElemAt: ["$goals", "$idx"] },
durations: { $arrayElemAt: ["$durations", "$idx"] }
}
}
)
Run Code Online (Sandbox Code Playgroud)
执行以下数据(示例文档):
{
"_id" : ObjectId("52d017d4b60fb046cdaf4851"),
"dates" : [
1399518702000,
1399126333000,
1399209192000,
1399027545000
],
"dress_number" : "4",
"name" : "J. Evans",
"numbers" : [
"5982",
"5983",
"5984",
"5985"
],
"goals": [
"1",
"0",
"4",
"2"
],
"durations": [
"78",
"45",
"90",
"90" …Run Code Online (Sandbox Code Playgroud) mongodb mongodb-query aggregation-framework mongodb-aggregation
当我对我的收藏进行此查询时...
models.Project.find(function(err, result) {
//result = doc below
}).populate('media')
Run Code Online (Sandbox Code Playgroud)
...我得到这个结果:
{
_id: 57f36baa6cf34d079c8474a0,
code: 'ZMIA',
__v: 0,
media:[
{
_id: 57f36bb26cf34d079c847766,
project_code: 'ZMIA',
amount: 228,
__v: 0
},
{
_id: 57f36bb26cf34d079c84775c,
project_code: 'ZMIA',
amount: 250,
__v: 0
}
]
},
{
_id: 57f36baa6cf34d079c8474a1,
code: 'ZMJU',
__v: 0,
media: []
}
Run Code Online (Sandbox Code Playgroud)
media是一个引用字段。如何将嵌套的媒体对象(如果存在)聚合到$sum该amount字段并将结果分组project_code?
mongoose mongodb node.js aggregation-framework mongodb-aggregation
我想对此架构执行查询:
{
"name" : "xxx",
"guests" : [
{
"category" : "category 1",
"arrived" : false
},
{
"category" : "category 2",
"arrived" : false
},
{
"category" : "category 1",
"arrived" : true
},
{
"category" : "category 2",
"arrived" : true
}
]
}
Run Code Online (Sandbox Code Playgroud)
我想得到一个特定的名字,一个列表,列出每个类别的客人百分比.
对于上面的文档作为示例,我想收到:
{
name : "xxx",
results : [
{
category : "category 1",
arrived : 50 (percent)
},
{
category : "category 2",
arrived : 50 (percent)
},
]
Run Code Online (Sandbox Code Playgroud)
有没有办法用一个MongoDB查询来做到这一点?另外,我应该在客户端还是服务器端进行此计算?
在我的查询中,concat关键字不起作用,它返回null。
这是查询:-
db.leads.aggregate([
{$project:{
_id:0,
status:1,
stage:1,
"todo.title":1,
created:{
day:{$substr:["$createdOn",8,2]},
month:{$substr:["$createdOn",5,2]},
year:{$substr:["$createdOn",0,4]}
},
myDate:{$concat:["$created.day","-","$created.month","-","$created.day"]}
//----above $concat is not working-----//
//--i want that `myDate` should be "12-09-2016"----//
}
}
])
Run Code Online (Sandbox Code Playgroud)
这是查询输出:
{
"stage" : "Prospect",
"todo" : [],
"status" : "OPEN",
"created" : {
"day" : "12",
"month" : "09",
"year" : "2016"
},
"myDate" : null
//--here i want that `myDate` should be "12-09-2016"----//
}
Run Code Online (Sandbox Code Playgroud)
在mongodb 中将createdOn字段数据存储为Date类型,即
mongodb mongodb-query aggregation-framework mongodb-aggregation
我正在尝试按两个字段对文档进行分组,field1并且field2.
myCollection.aggregate([{
$group: {
_id: {
group1: "$field1",
group2: "$field2"
},
count: {
$sum: 1
}
}
}])
Run Code Online (Sandbox Code Playgroud)
效果很好,可以产生预期的结果。
但我想在循环中重新运行上面的代码,每次运行都会有不同的结果$field2,所以下面的代码失败了,我该怎么办?谢谢
myCollection.aggregate([{
$group: {
_id: {
group1: "$field1",
group2: "$field2"
},
count: {
$sum: 1
}
}
}])
Run Code Online (Sandbox Code Playgroud) javascript mongodb mongodb-query aggregation-framework mongodb-aggregation
我有以下文档结构:
{
_id: 123,
name: 'My playlist',
videos:[
{videoId:1},
{videoId:2},
{videoId:3}]
}
Run Code Online (Sandbox Code Playgroud)
现在我想$lookup在视频集合中做一个来获取所有视频数据。最后,我需要一个这样的数据结构:
{
_id: 123,
name: 'My playlist',
videos:[
{videoId:1, videoDetails:[{_id:1, title:'My funny video', views:123}]},
{videoId:2, videoDetails:[{_id:2, title:'My new video', views:1234}]},
{videoId:3, videoDetails:[{_id:3, title:'Another video', views:1236}]}]
}
Run Code Online (Sandbox Code Playgroud)
这可能与 MongoDB 3.2 和$lookup聚合有关吗?