【发布时间】:2017-06-27 13:39:00
【问题描述】:
我正在构建一个在不同网页之间旋转的仪表板。我想拉出属于“测试”甲板的所有幻灯片并适当地订购它们。查询后,我的结果将是理想的样子。
[
{ "url" : "http://10.0.1.187", "position": 1, "duartion": 10 },
{ "url" : "http://10.0.1.189", "position": 2, "duartion": 3 }
]
我目前有一个如下所示的数据集
{
"_id" : ObjectId("53a612043c24d08167b26f82"),
"url" : "http://10.0.1.189",
"decks" : [
{
"title" : "Test",
"position" : 2,
"duration" : 3
}
]
}
{
"_id" : ObjectId("53a6103e3c24d08167b26f81"),
"decks" : [
{
"title" : "Test",
"position" : 1,
"duration" : 2
},
{
"title" : "Other Deck",
"position" : 1,
"duration" : 10
}
],
"url" : "http://10.0.1.187"
}
我尝试的查询如下所示:
db.slides.aggregate([
{
"$match": {
"decks.title": "Test"
}
},
{
"$sort": {
"decks.position": 1
}
},
{
"$project": {
"_id": 0,
"position": "$decks.position",
"duration": "$decks.duration",
"url": 1
}
}
]);
但这并没有产生我想要的结果。如何查询我的数据集并以最佳方式获得预期结果?
【问题讨论】:
标签: mongodb mongodb-query aggregation-framework