我試圖通過 objectId 型別的欄位doctorId獲取,但是它總是回傳一個空陣列。我在沒有過濾器的情況下對其進行了測驗,它作業正常,所以我認為這不是涉及模型或連接的錯誤:摩卡測驗:
mongoose.connect('mongodb://localhost:27017/test')
chai.should()
describe('Get treatments by doctorId', () => {
it("get two entries", async () => {
const treatments = await repository.getByDoctorId(new mongoose.Types.ObjectId("6260a98cbad424eec4818556"))
console.log(treatments)
treatments.should.not.be.empty
})
})
我用來獲取資料的函式:
const getByDoctorId = async (doctorId) =>{
console.log(doctorId)
try{
return await Treatment.find({doctorId: doctorId})
}catch(ex){
return null
}
}
我要獲取的資料:

uj5u.com熱心網友回復:
根據您顯示的影像,doctorId不是. 這是一個。看看它和(那是一個)之間的區別。ObjectIdstring_idObjectId
它顯然具有與 a 相同的格式ObjectId,但是像過濾一樣過濾預計不會產生任何結果。MongoDBObjectId與 a 不同string,即使它們看起來相同。
要獲得您想要的行為,只需將您的存盤庫呼叫更改為:
const treatments = await repository.getByDoctorId("6260a98cbad424eec4818556")
這應該夠了吧。
注意:避免在影像中顯示類似的資訊。它通常會嚇跑幫助。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/465233.html
下一篇:mongodb在陣列中查找欄位
