简体   繁体   English

如何修改MongoDB中添加的动态文档字段

[英]How to modify added dynamically document fields in MongoDB

I'm using MEAN.io for a project. 我正在使用MEAN.io来完成一个项目。 In MEAN.io, the package for managing the users is located in node_modules so it mustn't be modified. 在MEAN.io中,用于管理用户的包位于node_modules中,因此不得修改它。 The MongoDB model for users is located there so if I want to add new fields I have to do it by using schema.add method from my own custom created packages: 用户的MongoDB模型位于那里,所以如果我想添加新字段,我必须使用我自己创建的包中的schema.add方法来完成:

user.schema.add({
    field1: [],
    deleted_at: {
      type: Date,
      default: null
    },
    banned_at: {
      type: Date,
      default: null
    },
    created_at: {
      type: Date,
      default: new Date()
    },
    field2: {
      type: Number,
      default: 0
    },
    field3: {
      type: Number,
      default: 0
    },
    field4: {
      type: Number,
      default: 0
    }
});

Everything seems to work pretty well, but when I've tried to modify the deleted_at field doing something like this: 一切似乎都运行得很好,但是当我尝试修改deleted_at字段时,执行以下操作:

user.deleted_at = new Date();
console.log(user);

User object doesn't show any trace of the deleted_at field. 用户对象不显示deleted_at字段的任何跟踪。 It doesn't just change anything. 它不只是改变任何东西。

So the question is how can I modify a field added dynamically? 所以问题是如何修改动态添加的字段?

Thanks in advance! 提前致谢!

Problem is that by design schemas are compiled into models and cannot be edited dynamically. 问题是设计模式被编译成模型而无法动态编辑。 This thread ( here ) contains the information about mongoose but the same is true for other methods of adding field dynamically to the database schema. 此线程( 此处 )包含有关mongoose的信息,但对于将字段动态添加到数据库模式的其他方法也是如此。

You should use mixed types if you are not sure about your schema. 如果您不确定架构,则应使用混合类型。 Go through @vkarpov15 comments for other options. 浏览@ vkarpov15评论以获取其他选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM