我有一个名为example
example = new Mongo.Collection("example");
example
包含我想要更新的各种不同字段,但在更新时我还想包含新字段。
这可能吗?
答案 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",
}
});