在mongodb中按汇总请求按日期排序 [英] sort by date with aggregate request in mongodb
问题描述
我想检索当前已签署的最旧文档的值列表,但是我未能选择该日期缺席的文档.谢谢
I would like to retrieve a list of values that comes from the oldest document currently signed.But i failed to select a document absed on the date.Thanks
这是json:
"ad" : "noc3",
"createdDate" : ISODate(),
"list" : [
{
"id" : "p45",
"value" : 21,
},
{
"id" : "p6",
"value" : 20,
},
{
"id" : "4578",
"value" : 319
}
]
这是我的汇总请求:
db.friends.aggregate({$match:{advertiser:"noc3", {$sort:{timestamps:-1},{$limit:1} }},{$unwind:"$list"},{$project:{_id: "$list.id", value:{$add:[0]}}});
推荐答案
您的汇总查询不正确.您将排序和限制添加到匹配项中,但这就是您的操作方式.您使用不同的管道运算符:
Your aggregate query is incorrect. You add the sort and limit to the match, but that's now how you do that. You use different pipeline operators:
db.friends.aggregate( [
{ $match: { advertiser: "noc3" } },
{ $sort: { createdDate: -1 } },
{ $limit: 1 },
您的其他管道运算符也有些奇怪,并且您的代码与查询在timestamps
与createdDate
上不匹配.如果您添加了预期的输出,那么我也可以更新答案以包括查询的最后几位.
Your other pipeline operators are bit strange too, and your code vs query mismatches on timestamps
vs createdDate
. If you add the expected output, I can update the answer to include the last bits of the query too.
这篇关于在mongodb中按汇总请求按日期排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!