简体   繁体   English

mongoose findOneAndUpdate仅附加对象

[英]mongoose findOneAndUpdate appends objects only

I have an object like follows; 我有一个像下面这样的对象;

var exObj = { 'title' : 'name1'};

some of them have a data property of objects (not an array as I want to reference by name) and look like 其中一些有对象的数据属性(不是我希望通过名称引用的数组),看起来像

  exObj = {
    title : 'name2',
    data : {
      prop1 : 'prop1',
      prop2 : 'prop2'
    }
  }

Now I want to add another property to data, sometimes the data property will exist, and sometimes not, but I want to append a property (addedProp) and save it so I will end up with this; 现在我想为数据添加另一个属性,有时数据属性将存在,​​有时不存在,但我想附加一个属性(addedProp)并保存它以便我最终得到这个;

 exObj = {
    title : 'name2',
    data : {
      prop1 : 'prop1',
      prop2 : 'prop2',
      addedProp : 'value'
    }
  }

When using findOneAndUpdate I can only seem to pass in a whole object which is then appended; 当使用findOneAndUpdate时,我似乎只能传入一个随后附加的整个对象; something like this is what I currently do. 这样的事情就是我现在所做的。

var data = {};
data.addedProp = value;

Collection.findOneAndUpdate({
  title: 'name2'
}, {
  data
}, {
  upsert: true
})
.exec(function(err) {
  if (err) {
    console.log(err);
  } else {
    console.log('Updated');
  }
});

But obviously this overides the data object that exists; 但显然这会覆盖存在的数据对象; what is the correct way to do a findOneAndUpdate and make more meaningful changes to the object? 执行findOneAndUpdate并对对象进行更有意义的更改的正确方法是什么? I tried casting to toObject() but then I don't have a proper mongoose object to do a .save() on. 我尝试转换为toObject(),但后来我没有一个正确的猫鼬对象来执行.save()。

To add further clarification; 进一步澄清; which this worked for simple properties (and worked well) which I know are set; 这适用于我所知道的简单属性(并且运行良好); there are some values I wish to add which I need to check if they have a value for the property before adding the property . 我想添加一些值,我需要在添加属性之前检查它们是否具有该属性的值

So something like this; 所以这样的事情;

    Collection.findOneAndUpdate({
      field: val
      }, {
      if (tObj.title) {
        title = tObj.title;
      }

      if (tObj.date) {
        release_date = tObj.date;
      }

      if (tObj.name) {
        name = tObj.name;
      }
    }, { upsert: true
    })
    .exec(function(err) {
    if (err) {
     //handler
    } else {
    //handler
    }
  });

Your question first seem daunting, but solution is quite simple, you don't need to use $ or upsert, because you are not using any array , so don't need of positional operator or upsert. 你的问题首先看起来令人生畏,但解决方案非常简单, 你不需要使用$或upsert,因为你没有使用任何数组,所以不需要位置运算符或upsert。 You can use below code. 您可以使用以下代码。

Collection.findOneAndUpdate({
  title: 'name2'
},{
    $set:{"data.prop3":"new prop3"}
})
.exec(function(err) {
  if (err) {
    console.log(err);
  } else {
    console.log('Updated');
  }
});

It will add prop3 if not exists, or if it exists it will update it. 如果不存在,它将添加prop3,或者如果它存在,它将更新它。 I have checked the code on my local db with update and findOneAndUpdate 我已使用update和findOneAndUpdate检查了本地数据库上的代码

You need to use dot notation to target specific fields within an embedded object, building up your update object based on the supplied fields: 您需要使用点表示法来定位嵌入对象中的特定字段,并根据提供的字段构建更新对象:

var update = {};
if (tObj.title) {
    update.title = tObj.title;
}
if (tObj.data) {
    if (tObj.data.prop1) {
        update['data.prop1'] = tObj.data.prop1;
    }
    if (tObj.data.prop2) {
        update['data.prop2'] = tObj.data.prop2;
    }
    if (tObj.data.addedProp) {
        update['data.addedProp'] = tObj.data.addedProp;
    }
}

Collection.findOneAndUpdate({
  title: 'name2'
}, {
  $set: update
}, {
  upsert: true
})
.exec(function(err) {

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

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