Mongodose架构更新不是由Mongodb数据库获取的

时间:2022-12-02 02:34:34

I've my db already created where I have the following schema:

我已经创建了我的数据库,我有以下架构:

const ProjectSchema = new mongoose.Schema({
    name: { type: String },
    description: { type: String },
    client: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'client'
    },
    group: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'project_group'
    }
});

I need to change the schema to

我需要将架构更改为

const ProjectSchema = new mongoose.Schema({
    name: { type: String, unique: true, required: true },
    description: { type: String },
    client: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'client'
    },
    group: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'project_group'
    }
});

because we need to force name to be unique and not null. After change this definition, I see that the db still saving documents with the same name value. My question is, how can I apply any change I've done in my schema? and, how can I do that automatically without doing this manually?

因为我们需要强制name是唯一的而不是null。更改此定义后,我看到db仍然保存具有相同名称值的文档。我的问题是,如何应用我在架构中所做的任何更改?而且,如果不手动执行此操作,我该如何自动执行此操作?

Regards

问候

1 个解决方案

#1


0  

You most likely need to index that field so it can be quickly searched. Then also restart your server, if you haven't already. It's easy to do if you have MongoDB Compass (the official MongoDB GUI), or you can read this doc. https://docs.mongodb.com/manual/core/index-single/#create-an-index-on-an-embedded-field

您很可能需要索引该字段,以便快速搜索。然后还要重新启动服务器,如果还没有。如果您有MongoDB Compass(官方MongoDB GUI),这很容易,或者您可以阅读此文档。 https://docs.mongodb.com/manual/core/index-single/#create-an-index-on-an-embedded-field

#1


0  

You most likely need to index that field so it can be quickly searched. Then also restart your server, if you haven't already. It's easy to do if you have MongoDB Compass (the official MongoDB GUI), or you can read this doc. https://docs.mongodb.com/manual/core/index-single/#create-an-index-on-an-embedded-field

您很可能需要索引该字段,以便快速搜索。然后还要重新启动服务器,如果还没有。如果您有MongoDB Compass(官方MongoDB GUI),这很容易,或者您可以阅读此文档。 https://docs.mongodb.com/manual/core/index-single/#create-an-index-on-an-embedded-field