0
我有以下MongoDB的文件:是否可以查詢MongoDB文檔並將其分組到一個子文檔數組中?
{
"_id" : ObjectId("58950c9c247430c8d9f3997c"),
"stamp_dt" : ISODate("2015-10-15T00:09:00.000+0000"),
"start_lat" : 52.49512579479001,
"asimId" : ObjectId("588fbe3b835262b7f5ede6f5")
}
{
"_id" : ObjectId("58950c9c247430c8d9f3997d"),
"stamp_dt" : ISODate("2015-10-15T00:09:00.000+0000"),
"start_lat" : 52.48510698285751,
"asimId" : ObjectId("588fbe3b835262b7f5ede6f5")
}
{
"_id" : ObjectId("58950c9c247430c8d9f3997e"),
"stamp_dt" : ISODate("2015-10-15T00:09:00.000+0000"),
"start_lat" : 52.49966542235,
"asimId" : ObjectId("588fbe3b835262b7f5ede6f5")
}
{
"_id" : ObjectId("58950c9c247430c8d9f3997f"),
"stamp_dt" : ISODate("2015-10-15T00:09:00.000+0000"),
"start_lat" : 52.52645820975653,
"asimId" : ObjectId("588fbe3b835262b7f5ede6f5")
}
注意,都對stamp_dt
相同的值。我需要一種方法來組這些文件是這樣的:
{
"stamp_dt" : ISODate("2015-10-15T00:09:00.000+0000"),
"results": [
{
"_id" : ObjectId("58950c9c247430c8d9f3997c"),
"start_lat" : 52.49512579479001,
"asimId" : ObjectId("588fbe3b835262b7f5ede6f5")
}
{
"_id" : ObjectId("58950c9c247430c8d9f3997d"),
"start_lat" : 52.48510698285751,
"asimId" : ObjectId("588fbe3b835262b7f5ede6f5")
}
{
"_id" : ObjectId("58950c9c247430c8d9f3997e"),
"start_lat" : 52.49966542235,
"asimId" : ObjectId("588fbe3b835262b7f5ede6f5")
}
{
"_id" : ObjectId("58950c9c247430c8d9f3997f"),
"start_lat" : 52.52645820975653,
"asimId" : ObjectId("588fbe3b835262b7f5ede6f5")
}
]
}
我可以使用代碼做到這一點,但有沒有辦法通過MongoDB的查詢(也許聚集)這樣做呢?
你可以嘗試這樣的事情。 'db.collection.aggregate( \t [{ \t \t $組:{ \t \t \t _id: 「$ stamp_dt」, \t \t \t 「結果」:{ \t \t \t \t $推:{ \t \t \t \t \t _id: 「$ _id」, \t \t \t \t \t start_lat: 「$ start_lat」 \t \t \t \t \t asimId = 「$ asimId」 \t \t \t \t} \t \t \t} \t \t} \t}] )' – Veeram
謝謝!這工作。如果您想將其作爲答案發布,我會將其標記爲答案並給予您信任。 –