1
我想根據以下模式獲取我的集合中的特定條目。MongoDB查找具有不同字段的項目
它musst相同objectClass
由version
排序,以獲得最新的過濾,然後讓只objectId
最新的(注:這不是項目ID!)
我已經嘗試了一些組合具有鮮明和組,但不得到它
集(歷史):
{ "_id" : ObjectId("AAA"), "objectId" : ObjectId("BBB"), "objectClass" : "AAA\BBB\CCC", "version" : NumberInt("1"), "fields" : {...} }
{ "_id" : ObjectId("AAB"), "objectId" : ObjectId("BBB"), "objectClass" : "AAA\BBB\CCC", "version" : NumberInt("2"), "fields" : {...} }
{ "_id" : ObjectId("AAC"), "objectId" : ObjectId("BBB"), "objectClass" : "AAA\BBB\CCC", "version" : NumberInt("3"), "fields" : {...} }
{ "_id" : ObjectId("AAD"), "objectId" : ObjectId("BBA"), "objectClass" : "AAA\BBB\CCC", "version" : NumberInt("1"), "fields" : {...} }
{ "_id" : ObjectId("AAE"), "objectId" : ObjectId("BBA"), "objectClass" : "AAA\BBB\CCC", "version" : NumberInt("2"), "fields" : {...} }
{ "_id" : ObjectId("AAF"), "objectId" : ObjectId("BBA"), "objectClass" : "AAA\BBB\CCC", "version" : NumberInt("3"), "fields" : {...} }
我現在聲明(不是返回預期的結果):
db.history.find({objectClass: "AAA\BBB\CCC"}).sort({version: -1});
預期結果:
[
{ "_id" : ObjectId("AAC"), "objectId" : ObjectId("BBB"), "objectClass" : "AAA\BBB\CCC", "version" : NumberInt("3"), "fields" : {...} }
{ "_id" : ObjectId("AAF"), "objectId" : ObjectId("BBA"), "objectClass" : "AAA\BBB\CCC", "version" : NumberInt("3"), "fields" : {...} }
]
您錯過了一個'}'和'$ mach',但工作很棒!謝謝! +1 –
@JasonSchilling謝謝你指出我錯過了什麼。我回去修復查詢 – Woodsy