В инфраструктуре агрегации SailsJS & MongoDB проблемы с пользовательскими запросами
Я совершенно новичок в MongoDB и пришел из SQL фона.
Я пытаюсь сделать это:
Get the top Artists, based on the number of Dubs.
Структура данных :
Artists = [
{
"dubs": [{...},{...},{...}],
"name": "The Doors",
"createdAt": "2014-12-15T15:24:26.216Z",
"updatedAt": "2014-12-15T15:24:26.216Z",
"id": "548efd2a436c850000353f4f"
},
{
"dubs": [],
"name": "The Beatles",
"createdAt": "2014-12-15T20:30:33.922Z",
"updatedAt": "2014-12-15T20:30:33.922Z",
"id": "548f44e90630d50000e2d61d"
},
{...}
]
Таким образом, результат, который я ищу, будет примерно таким:
[{
_id: "548ef6215755950000a9a0de",
name:"The Doors",
total: 3
},{
_id: "548ef6215715300000a9a1f9",
name:"The Beatles",
total: 0
}]
Я пытался :
Artist.native(function(err, collection) {
collection.aggregate([ {
$group: {
_id: {
name: "$name"
},
total: {
$size: "$dubs"
}
}
}, {
$size: {
total: -1
}
}], function(e, r) {
if (e) res.serverError(e);
console.log(r);
});
});
Что дает мне
[]
А также :
Artist.native(function(err, collection) {
if (err) return res.serverError(err);
collection.aggregate({
$group: {
_id: "$name",
total: {
$sum: 1
}
}
}, {
$sort: {
total: -1
}
}, function(e, r) {
console.log(r);
if (e) return res.serverError(e);
});
});
Что дает мне
[ { _id: 'The Beatles', total: 1 },
{ _id: 'The Doors', total: 1 } ]
Спасибо