我正在使用 mongoDB、mongoose 和 typescript,我需要在查詢時保留檔案 ID,但我只能獲取型別 _id: new ObjectId("62aa4bddae588fb13e8df552") 。我只需要保留字串“62aa4bddae588fb13e8df552”以供以后存盤并進行其他處理。我無法擺脫新的 ObjectId
async findById(id:string) {
const convert = {"_id":id}
const userfindById = await userModel.findById(convert);
const iD = userfindById?._id
return userfindById;
}
uj5u.com熱心網友回復:
ObjectId只是一個型別:
https://www.mongodb.com/docs/manual/reference/bson-types/#std-label-objectid
如果要獲取可以使用 提取的字串_id.toString(),如果要存盤字串,則應更改的型別_id(或創建新屬性)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/492157.html
