Node.js mongoDB中的组数据
我正在尝试按repo Id对从mongoDB获得的数据进行分组 我的收藏结构是:Node.js mongoDB中的组数据,node.js,mongodb,express,Node.js,Mongodb,Express,我正在尝试按repo Id对从mongoDB获得的数据进行分组 我的收藏结构是: { "id":"8820624457", "type":"CreateEvent", "actor":{ "id":27394937, "login":"Johnson0608", "display_login":"Johnson0608", "gravatar_id":"", "url":"https://api.github.com
{
"id":"8820624457",
"type":"CreateEvent",
"actor":{
"id":27394937,
"login":"Johnson0608",
"display_login":"Johnson0608",
"gravatar_id":"",
"url":"https://api.github.com/users/Johnson0608",
"avatar_url":"https://avatars.githubusercontent.com/u/27394937?"
},
"repo":{
"id":163744671,
"name":"Johnson0608/test",
"url":"https://api.github.com/repos/Johnson0608/test"
},
"payload":{
"ref":"master",
"ref_type":"branch",
"master_branch":"master",
"description":null,
"pusher_type":"user"
},
"public":true,
"created_at":"2019-01-01T15:00:00Z"
}
我的代码是:
collection.find({}).project({ 'repo.id': 1, 'actor.login': 1, 'type': 1 }).toArray(function (err, docs) {
assert.equal(err, null);
console.log("Found the following records");
res.status(200).json({ docs });
callback(docs);
});
我正在尝试按repo id进行分组,但我不知道如何使用该功能(我是mongoDB新手)您可以使用该功能
按指定的\u id表达式对输入文档进行分组,对于每个不同的分组,输出文档。每个输出文档的\u id字段包含唯一的group by值
收藏
.find({})
.合计({
$group:{
_id:'repo.id'//您想要分组的对象
//…其他论点
}
})
.project({'repo.id':1,'actor.login':1,'type':1})
.toArray(函数(错误、文档){
assert.equal(err,null);
console.log(“找到以下记录”);
res.status(200).json({docs});
回拨(文件);
});
转到此
您希望分组后的数据采用哪种格式?by
repo.id
?@GaurangDhorda{“repo”:{“id”:163744671},“actors”:[{“login”:“Johnson0608”}]}欢迎:)请,然后再投票回答。
db.collection.aggregate([
{
$group: {
_id: "$repo.id",
Actors: {
$push: {
"login": "$actor.login"
}
}
}
}
])