嗨我在mongodb中有以下数据结构:
{
"_id" : "4087322f-1ad0-4595-935f-b41ef6d87306",
"lastModifiedDate" : ISODate("2016-11-22T20:48:05.904Z"),
"notes" : " I do not like him that much",
"peopleId" : "1121",
"status" : "asses"
}
我希望查询数据,使其返回每个peopleId的最新值,该值也与所选状态匹配。我一直在研究$ group,但是做同等地位会导致一些问题。
我尽可能使用peopleId对值进行分组:
.aggregate([
{$group : {_id : "$peopleId", "allData" : {$push : "$$ROOT"}}},
{$sort : { lastModifiedDate : -1}}
]).pretty()
给我这些结果:
{
"_id" : "1123",
"allData" : [
{
"_id" : "c9d6a6ce-104d-414a-8d89-512e556d7aba",
"lastModifiedDate" : ISODate("2016-11-22T20:55:35.662Z"),
"notes" : "He's in!",
"peopleId" : "1123",
"status" : "done"
},
{
"_id" : "14e522d8-6cae-42ee-ad52-d5cf3cae0c29",
"lastModifiedDate" : ISODate("2016-11-22T20:56:46.128Z"),
"notes" : "He's in!",
"peopleId" : "1123",
"status" : "asses"
}
]
}
{
"_id" : "1121",
"allData" : [
{
"_id" : "d2c3e5da-8696-4dcc-a5cd-1f2657f0192c",
"lastModifiedDate" : ISODate("2016-11-22T20:46:35.097Z"),
"notes" : " I do not like him",
"peopleId" : "1121",
"status" : "pending"
},
{
"_id" : "4087322f-1ad0-4595-935f-b41ef6d87306",
"lastModifiedDate" : ISODate("2016-11-22T20:48:05.904Z"),
"notes" : " I do not like him that much",
"peopleId" : "1121",
"status" : "asses"
},
{
"_id" : "be4fa5bd-da8c-4c1a-9010-6a86afd11dbc",
"lastModifiedDate" : ISODate("2016-11-22T20:49:27.268Z"),
"notes" : "He's in!\nTue Nov 22 12:49:27 PST 2016 He's TF OUT",
"peopleId" : "1121",
"status" : "done"
}
]
}
由此我需要根据lastModifiedDate提取最新的条目,然后检查它是否与给定的状态匹配。
答案 0 :(得分:1)
以下是解决方案。
解决方案1: -
如果您想要输出中集合中的所有字段。
db.collection.aggregate([
{$match : {"status" : "asses"}},
{$sort : {peopleId : 1, lastModifiedDate : -1}},
{$group : {_id : "$peopleId", "allData" : {$push : "$$ROOT"}}},
{$project: {_id :0, allData : {$slice : ["$allData", 0, 1]}}}
]);
<强>输出: - 强>
{
"allData" : [
{
"_id" : ObjectId("5834c004ba41f1f22e600c7f"),
"lastModifiedDate" : ISODate("2016-11-24T20:48:05.904Z"),
"notes" : " I do not like him that much",
"peopleId" : "1",
"status" : "asses"
}
]
}
解决方案2: -
如果您不想要输出中集合中的所有字段。
db.collection.aggregate([
{$match : {"status" : "asses"}},
{$sort : {peopleId : 1, lastModifiedDate : -1}},
{$group : {_id : "$peopleId", lastModDate : {$first : "$lastModifiedDate"}}}
])
<强>输出: - 强>
{
"_id" : "1",
"lastModDate" : ISODate("2016-11-24T20:48:05.904Z")
}
答案 1 :(得分:0)
我创建了一个包含以下文档的集合
MatchCollection matches = Regex.Matches(sString, @"<b>(.*)<\/b>", RegexOptions.Singleline);
如果我执行以下查询
{ "_id" : "4087322f-1ad0-4595-935f-b41ef6d87306", "lastModifiedDate" : ISODate("2016-11-22T20:48:05.904Z"), "notes" : " I do not like him that much",
"peopleId" : "1121", "status" : "asses" }
{ "_id" : "4087322f-1ad0-4595-935f-b41ef6d87305", "lastModifiedDate" : ISODate("2016-10-22T20:48:05.904Z"), "notes" : " I do not like him that much",
"peopleId" : "1121", "status" : "asses" }
{ "_id" : "4087322f-1ad0-4595-935f-b41ef6d87304", "lastModifiedDate" : ISODate("2016-11-21T20:48:05.904Z"), "notes" : " I do not like him that much",
"peopleId" : "1121", "status" : "asses" }
它给了我
db.mel.find().sort({lastModifiedDate:-1}).limit(1)
根据lastmodifeddate最新。