我正在尝试使用大型mongoDB自学mongoDb(每个文档总共大约1000万个文档)
我想尝试一些基础知识。例如,列出通过UsedCallories对所有用户进行排序的每个Activity。
db.getCollection('users').aggregate([
{$group: {_id:"$Activities"}},
{$sort: { UsedCallories: -1}}
],{allowDiskUse:true});
不幸的是,当我执行这个脚本时,它给了我:'脚本执行成功,但没有结果显示。'?
你能指点一下我错了吗?
缩短的示例文件:
{
"Id": 1,
"FirstName": "Casie",
"LastName": "Crapo",
"Email": "Casie.Crapo@databanken.db",
"Weight": 92,
"Length": 198,
"Activities": [
{
"ActivityType": {
"Name": "Sexual Activity",
"CallPerSecond": 0.033333333
},
"StartCoordinates": {
"Lattidude": -10.81907,
"Longitude": -16.16832
},
"EndCoordinates": {
"Lattidude": -10.81907,
"Longitude": -16.16832
},
"StartDateTime": { $date: "2016-11-01T23:39:15Z" },
"EndDateTime": { $date: "2016-11-02T02:38:45Z" },
"UsedCallories": 772.63042705630426,
"Measurements": [
{
"Heartrate": 142,
"UnderPressure": 123,
"Overressure": 156,
"Speed": 0,
"Coordinates": {
"Lattidude": -10.81907,
"Longitude": -16.16832
}
}
]
}
]
}
更新'ExpectedOutput':
因此,预期输出只是用户所有数组字段中所有活动的列表。在UsedCallories上排序。
"Activities": [
{
"ActivityType": {
"Name": "Sexual Activity",
"CallPerSecond": 0.033333333
},
"StartCoordinates": {
"Lattidude": -10.81907,
"Longitude": -16.16832
},
"EndCoordinates": {
"Lattidude": -10.81907,
"Longitude": -16.16832
},
"StartDateTime": { $date: "2016-11-01T23:39:15Z" },
"EndDateTime": { $date: "2016-11-02T02:38:45Z" },
"UsedCallories": 772.63042705630426,
"Measurements": [
...
]
},{
"ActivityType": {
"Name": "Sexual Activity",
"CallPerSecond": 0.033333333
},
"StartCoordinates": {
"Lattidude": -10.81907,
"Longitude": -16.16832
},
"EndCoordinates": {
"Lattidude": -10.81907,
"Longitude": -16.16832
},
"StartDateTime": { $date: "2016-11-01T23:39:15Z" },
"EndDateTime": { $date: "2016-11-02T02:38:45Z" },
"UsedCallories": 52.63042705630426,
"Measurements": [
...
]
},{
"ActivityType": {
"Name": "Sexual Activity",
"CallPerSecond": 0.033333333
},
"StartCoordinates": {
"Lattidude": -10.81907,
"Longitude": -16.16832
},
"EndCoordinates": {
"Lattidude": -10.81907,
"Longitude": -16.16832
},
"StartDateTime": { $date: "2016-11-01T23:39:15Z" },
"EndDateTime": { $date: "2016-11-02T02:38:45Z" },
"UsedCallories": 20.22442,
"Measurements": [
...
]
}
]
重复提问后更新
好的,感谢您对重复帖子的引用。但这不是同一个问题。
我设法使用其中的一部分来实际获得一些结果。 查询更改为:
db.getCollection('users').aggregate([
{$unwind: '$Activities'},
{$sort: {'Activities.UsedCallories': -1}},
{$group: {_id: '$_id', 'Activities': {$push: '$Activities'}}}
], {
allowDiskUse:true
})
现在返回GROUPED BY USER的所有活动,我更喜欢所有这些活动的LIST,而不是按用户分组
答案 0 :(得分:0)
谢谢@chridam。添加我的评论作为答案。
db.getCollection('users').aggregate([{
$unwind: "$Activities"
}, {
$sort: {
"Activities.UsedCallories": -1
}
}, {
$group: {
_id: null,
Activities: {
$push: "$Activities"
}
}
}, {
$project: {
_id: 0,
Activities: 1
}
}], {
allowDiskUse: true
});