0
我的收藏夾包含以下記錄。如何獲取mongodb中每組記錄的最新記錄
{
"_id" : "58a53f4aca3d39bcf9c3890a",
"code" : 1,
"datetime":"2017-01-01T00:00:00.000Z",
"balance" : 2000
}
{
"_id" : "58a53f4aca3d39bcf9c3890b",
"code" : 2,
"datetime":"2017-01-02T00:00:00.000Z",
"balance" : 3000
}
{
"_id" : "58a53f4aca3d39bcf9c3890c",
"code" : 1,
"datetime":"2017-01-03T00:00:00.000Z",
"balance" : 3000
}
{
"_id" : "58a53f4aca3d39bcf9c3890d",
"code" : 1,
"datetime":"2017-01-04T00:00:00.000Z",
"balance" : 4000
}
{
"_id" : "58a53f4aca3d39bcf9c3890e",
"code" : 2,
"datetime":"2017-01-05T00:00:00.000Z",
"balance" : 7000
}
{
"_id" : "58a53f4aca3d39bcf9c3890f",
"code" : 1,
"datetime":"2017-01-06T00:00:00.000Z",
"balance" : 6000
}
我正在基於代碼製作一組記錄。根據上述記錄,只有2組代碼:1和代碼:2。
,所以我想以下內容作爲編碼1的最新記錄輸出和CODE2
{ "_id" : "58a53f4aca3d39bcf9c3890f", "code" : 1,"datetime":"2017-01-06T00:00:00.000Z", "balance" : 6000 }
{ "_id" : "58a53f4aca3d39bcf9c3890e", "code" : 2,"datetime":"2017-01-05T00:00:00.000Z", "balance" : 7000 }
所以,我怎樣才能使查詢,這將給我上面的輸出?
您需要ta添加日期字段來實現此目的,否則您將無法知道哪些文檔是最新的 – felix
@felix是的,我有每個記錄的日期時間字段,那麼我怎麼能得到該輸出。 –