我有以下架構:
const { ObjectId } = Schema.Types
const Account = new Schema({
name: { type: String, required: true },
accountId: { type: String, required: true },
piracicaba: [{ type: ObjectId, ref: 'Piracicaba'}]
})
我知道默認情況下 MongoDB 創建一個_id屬性,但我想改為使用accountId我的默認 ID。另外,我想將默認生成的字串更改為自定義字串。
我怎么能那樣做?
我試圖將accountId型別更改為ObjectId:
const { ObjectId } = Schema.Types
const Account = new Schema({
name: { type: String, required: true },
accountId: { type: ObjectId },
piracicaba: [{ type: ObjectId, ref: 'Piracicaba'}]
})
但是當我嘗試存盤我的自定義 ID mongoose throws 時BSONTypeError: Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer。
我的自定義 ID 來自 Oracle 云帳戶,如下所示:ocid1.test.oc1..<unique_ID>EXAMPLE-compartmentId-Value
uj5u.com熱心網友回復:
如果您_id在架構定義中包含一個欄位,那么當您插入一個檔案時,您必須為它提供您自己手動生成的_id. 如果不這樣做,檔案將不會被插入。
或者,如果您沒有_id在架構定義中包含欄位,Mongoose 將在插入檔案時自動為您創建它,并且它將是型別ObjectId(這是 MongoDB_id在檔案上設定欄位的默認方式)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/524650.html
標籤:节点.js猫鼬猫鼬模式
上一篇:將URL卷曲到vb.net
