我正在嘗試使用 findOne ( not find ) 在 Mongoose 中查找檔案,并且我想獲取最新的檔案,即使它們可能具有相同的欄位。
例如,這些是 Mongoose 中的檔案:
// Let's say this one was created a week ago in Mongoose
{
name: "John Smith",
age: 27,
industry: "finance"
}
// This one was created this morning
{
name: "John Smith",
age: 27,
industry: "healthcare"
}
所以我需要按照以下方式進行查詢:
const client = await Client.findOne({name: "John Smith"});
console.log(client);
/*
result:
{
name: "John Smith",
age: 27,
industry: "finance"
}
expected:
{
name: "John Smith",
age: 27,
industry: "healthcare"
}
*/
因此,當我Client.findOne點名時,我期待的是最新的檔案。然而Client.findOne似乎只是回傳舊的。
有什么建議嗎?
uj5u.com熱心網友回復:
將引數作為物件傳遞,sort: { 'created_at' : -1 }將 -1 更改為 1 將獲取最舊的記錄所以你的新查詢看起來像
const client = Client.findOne({name: "John Smith"}).sort({created_at: -1});
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/433496.html
標籤:javascript mongodb 猫鼬
