小编Rit*_*ika的帖子

更新架构而不删除 mongodb 中的集合

我的架构如下所示:

const UserSchema = Schema({
    _id: mongoose.Schema.Types.ObjectId,
    userName: {
        type: String,
        required: true,
    },
    userPhoneNumber: {
        type: String,
    },
    role:[{
        type:mongoose.Schema.Types.ObjectId,
        ref:Role
    }]
});
Run Code Online (Sandbox Code Playgroud)

通过这个模式,我在数据库中添加了文档role:[]。但现在根据要求,我需要更新架构而不删除现有集合。我需要更新数据,除了角色之外,所有字段都相同。我想将现有记录更新为角色

const UserSchema = Schema({
    _id: mongoose.Schema.Types.ObjectId,
    userName: {
        type: String,
        required: true,
    },
    userPhoneNumber: {
        type: String,
    },
    role:[{
        type:mongoose.Schema.Types.ObjectId,
        ref:Role
    }]
});
Run Code Online (Sandbox Code Playgroud)

那么如何在不删除集合的情况下更新架构。

schema mongoose mongodb node.js

6
推荐指数
1
解决办法
1603
查看次数

标签 统计

mongodb ×1

mongoose ×1

node.js ×1

schema ×1