简体   繁体   English

如果不在新文档中,Upsert会删除该字段

[英]Upsert removes field if it's not in the new document

Let's assume the following model (note strict:false ): 让我们假设以下模型(注意strict:false ):

var Test = db.model('Test', {
  a: {type: String, required: true},
  b: {type: String}
}, {strict: false});

and I currently have this document in the database: 我目前在数据库中有此文档:

{
  'a': 'hello',
  'b': 'world',
  'c': {
       'x': 'embedded',
       'z': 'document' 
     }
}

If I upsert the following document: 如果我upsert下列文件:

doc = {
  'a': 'hello',
  'b': 'jack'
}

with something like: 与类似:

Test.findOneAndUpdate({
  a: 'hello'
}, doc, {upsert:true}, function(){});

I will end up with: 我将得出以下结论:

{
  'a': 'hello',
  'b': 'jack'
}

In other words, a field c that is not present in the upserted document is wiped. 换句话说,擦除了在加稿的文档中不存在的字段c

How can I safely upsert without wiping such fields? 我如何才能安全地更新而不擦除这些字段?

Strange, this should work as is. 奇怪,这应该可以正常工作。 Maybe you can try explicitly setting fields: 也许您可以尝试显式设置字段:

doc = {$set: {
  'a': 'hello',
  'b': 'jack'
}};

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

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