我有这些文件,
{
"_id" : 1,
"fId" : "test",
"pId" : "test",
"url" : "A",
"subfolder" : "a"
}
,
{
"_id" : 3,
"fId" : "test",
"pId" : "test",
"url" : "A",
"subfolder" : "b"
}
,
{
"_id" : 5,
"fId" : "test",
"pId" : "test"
"url" : "A",
"subfolder" : "c"
}
,
{
"_id" : 2,
"fId" : "test",
"pId" : "test",
"url" : "B",
"subfolder" : "a"
}
,
{
"_id" : 4,
"fId" : "test",
"pId" : "test",
"url" : "B",
"subfolder" : "b"
}
,
{
"_id" : 7,
"fId" : "test",
"pId" : "test",
"url" : "A",
"subfolder" : "d"
}
,
{
"_id" : 8,
"fId" : "test",
"pId" : "test",
"url" : "B",
"subfolder" : "d"
}
,
{
"_id" : 9,
"fId" : "test",
"pId" : "test",
"url" : "A",
"subfolder" : "e"
}
,
{
"_id" : 10,
"fId" : "test",
"pId" : "test",
"url" : "B",
"subfolder" : "e"
}
,
{
"_id" : 6,
"fId" : "test",
"pId" : "test",
"url" : "B",
"subfolder" : "c"
}
如何根据_id,group by子文件夹编写mongoose查询来对这些文档进行排序,并返回最近的5个组。
预期结果是
group 1:
[{
"_id" : 10,
"fId" : "test",
"pId" : "test",
"url" : "B",
"subfolder" : "e"
},
{
"_id" : 9,
"fId" : "test",
"pId" : "test",
"url" : "A",
"subfolder" : "e"
}]
,
group 2:
[{
"_id" : 8,
"fId" : "test",
"pId" : "test",
"url" : "B",
"subfolder" : "d"
}
,
{
"_id" : 7,
"fId" : "test",
"pId" : "test",
"url" : "A",
"subfolder" : "d"
}]
,
group 3:
[{
"_id" : 6,
"fId" : "test",
"pId" : "test",
"url" : "B",
"subfolder" : "c"
}
,
{
"_id" : 5,
"fId" : "test",
"pId" : "test"
"url" : "A",
"subfolder" : "c"
}]
,
group 4:
[{
"_id" : 4,
"fId" : "test",
"pId" : "test",
"url" : "B",
"subfolder" : "b"
}
,
{
"_id" : 3,
"fId" : "test",
"pId" : "test",
"url" : "A",
"subfolder" : "b"
}]
,
group 5:
[{
"_id" : 2,
"fId" : "test",
"pId" : "test",
"url" : "B",
"subfolder" : "a"
}
,
{
"_id" : 1,
"fId" : "test",
"pId" : "test",
"url" : "A",
"subfolder" : "a"
}]
我尝试过查询
model.aggregate(
[
{$match : {fId : "test" , pId : "test"}},
{$group : {_id : "$subfolder", recording: { $push: "$$ROOT" } }},
{$skip : 5*(pageNumber-1)},
{$sort : {"_id": -1}},
{$limit : 5 }
]
)
我得到随机的5组,而不是最近的组,结果也没有根据_id进行排序。
答案 0 :(得分:1)
你在这里误解了跳过和限制的使用。试试这个:
model.aggregate(
[
{$match : {fId : "test" , pId : "test"}},
{$group : {_id : "$subfolder", recording: { $push: "$$ROOT" } }},
{$sort : {"recording._id": -1}},
{$limit: 5}
]
)
提供示例文档的输出:
{
"_id" : "5",
"recording" : [
{
"_id" : 9,
"fId" : "test",
"pId" : "test",
"url" : "A",
"subfolder" : "5"
},
{
"_id" : 10,
"fId" : "test",
"pId" : "test",
"url" : "B",
"subfolder" : "5"
}
]
}
{
"_id" : "4",
"recording" : [
{
"_id" : 7,
"fId" : "test",
"pId" : "test",
"url" : "A",
"subfolder" : "4"
},
{
"_id" : 8,
"fId" : "test",
"pId" : "test",
"url" : "B",
"subfolder" : "4"
}
]
}
{
"_id" : "3",
"recording" : [
{
"_id" : 5,
"fId" : "test",
"pId" : "test",
"url" : "A",
"subfolder" : "3"
},
{
"_id" : 6,
"fId" : "test",
"pId" : "test",
"url" : "B",
"subfolder" : "3"
}
]
}
{
"_id" : "2",
"recording" : [
{
"_id" : 3,
"fId" : "test",
"pId" : "test",
"url" : "A",
"subfolder" : "2"
},
{
"_id" : 4,
"fId" : "test",
"pId" : "test",
"url" : "B",
"subfolder" : "2"
}
]
}
{
"_id" : "1",
"recording" : [
{
"_id" : 1,
"fId" : "test",
"pId" : "test",
"url" : "A",
"subfolder" : "1"
},
{
"_id" : 2,
"fId" : "test",
"pId" : "test",
"url" : "B",
"subfolder" : "1"
}
]
}