使用新字段更新数据库

时间:2015-12-14 09:26:45

标签: mongodb

我有一个名为example

的集合
example = new Mongo.Collection("example");

example包含我想要更新的各种不同字段,但在更新时我还想包含新字段。

这可能吗?

2 个答案:

答案 0 :(得分:2)

更新: -

如果使用upsert:true的更新操作导致插入文档,则$ setOnInsert会将指定的值分配给文档中的字段。如果更新操作没有导致插入,$ setOnInsert什么都不做。

您可以为db.collection.update()或db.collection.findAndModify()方法指定upsert选项。

transf = impict.transform

答案 1 :(得分:1)

根据文件,这是可能的。

https://docs.mongodb.org/v3.0/reference/operator/update/set/

  

如果该字段不存在,$ set将添加一个新字段   指定的值,前提是新字段不违反类型   约束

      example.update(exampleId, {
        $set: {
         newfield: "new field information",
        }
      });
相关问题