簡體   English   中英

停止貓鼬創建自動索引

[英]Stop Mongoose to create auto index

我試圖在貓鼬中創建架構,它在mongodb中創建名為_id的新索引?

 const modelSchema = new Schema({
    uid: { type: Number, required: true },
    content: {
        text: { type: String },
        attachments: [{
            type: { type: String, enum: ['IMAGE', 'VIDEO', 'POLL', 'GOAL', 'POST', 'MILESTONE', 'ARTICLE', 'DOCUMENT', 'AUDIO', 'QUOTE', 'USER', 'TIP', 'URL'] },
            object_id: { type: Number }
        }]
    },
    created: { type: String }
});

我期望這個結果:

"content" : {
"text" : "hello",
"attachments" : [ 
    {
        "type" : "IMAGE",
        "object_id" : 1
    }
]

我得到這個結果:

"content" : {
"text" : "hello",
"attachments" : [ 
    {
        "type" : "IMAGE",
        "object_id" : 1,
        "_id" : ObjectId("5a2664fd6db6672ac43f1823")
    }
]

您可以將_id設置為false以在定義架構時禁用自動_id

var childSchema = new Schema({ name: String }, { _id: false });

檢查http://mongoosejs.com/docs/guide.html#_id

如果您想使用自己的替代值,可以將mongo創建為默認值_id

"_id" : 1

例如,如果您插入db.a.insert({"name":"abcd"})它將創建_id默認的db.a.insert({"name":"abcd","_id":1})並將_id設置為1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM