【发布时间】:2015-12-06 21:00:39
【问题描述】:
我有一个名为“plan”的 mongodb 集合,其中包含嵌套文档,如下所示:
//1 {
"_id" : ObjectId("55ed97c91ed85902375f60c3"),
"createdAt" : ISODate("2015-09-07T13:57:29.700Z"),
"suggestions" : [
{
"title" : "First suggestion",
"vote" : 5,
"_id" : ObjectId("55ed97c91ed85902375f60c5"),
"createdAt" : ISODate("2015-09-07T13:57:29.701Z"),
"startDate" : ISODate("2015-09-02T13:57:29.701Z"),
"endDate" : ISODate("2015-09-05T13:57:29.701Z")
},
{
"title" : "second suggestion",
"vote" : 8,
"_id" : ObjectId("55ed97c91ed85902375f60c4"),
"createdAt" : ISODate("2015-09-07T13:57:29.701Z"),
"startDate" : ISODate("2015-09-07T13:57:29.701Z"),
"endDate" : ISODate("2015-09-10T13:57:29.701Z")
}
],
"__v" : 0
},
//2 {
"_id" : ObjectId("55f1570fd86512e01f4d55c8"),
"createdAt" : ISODate("2015-09-10T10:10:23.381Z"),
"suggestions" : [
{
"title" : "First suggestion",
"vote" : 3,
"startDate" : ISODate("2015-09-10T13:57:29.701Z"),
"endDate" : ISODate("2015-09-15T13:57:29.701Z"),
"_id" : ObjectId("55f1570fd86512e01f4d55ca"),
"createdAt" : ISODate("2015-09-10T10:10:23.381Z")
},
{
"title" : "second suggestion",
"vote" : 2,
"startDate" : ISODate("2015-09-02T13:57:29.701Z"),
"endDate" : ISODate("2015-09-04T13:57:29.701Z"),
"_id" : ObjectId("55f1570fd86512e01f4d55c9"),
"createdAt" : ISODate("2015-09-10T10:10:23.381Z")
}
],
"__v" : 0
}
而且我必须收集“suggestions.endDate”小于“ISODate(”2015-09-07T13:57:29.701Z")”的集合,并且“投票”在集合中的所有建议中应该更大。我只期待这个:
//1 {
"_id" : ObjectId("55ed97c91ed85902375f60c3"),
"createdAt" : ISODate("2015-09-07T13:57:29.700Z"),
"suggestions" : [
{
"title" : "First suggestion",
"vote" : 5,
"_id" : ObjectId("55ed97c91ed85902375f60c5"),
"createdAt" : ISODate("2015-09-07T13:57:29.701Z"),
"startDate" : ISODate("2015-09-02T13:57:29.701Z"),
"endDate" : ISODate("2015-09-05T13:57:29.701Z")
},
{
"title" : "second suggestion",
"vote" : 8,
"_id" : ObjectId("55ed97c91ed85902375f60c4"),
"createdAt" : ISODate("2015-09-07T13:57:29.701Z"),
"startDate" : ISODate("2015-09-07T13:57:29.701Z"),
"endDate" : ISODate("2015-09-10T13:57:29.701Z")
}
],
"__v" : 0
},
这是我正在使用的查询:
db.plan.aggregate([{$match: {suggestions: {$elemMatch: {endDate: {$lt: ISODate("2015-09-07T13:57:29.701Z")}}}}}, {$sort: {"suggestions.vote": -1}}]).pretty()
但这对我不起作用,它只是匹配日期。我是 MongoDb 的新手?有什么帮助吗?谢谢。
【问题讨论】:
标签: node.js mongodb mongoose mongodb-query