简体   繁体   English

嵌套数组内的猫鼬findOneAndUpdate

[英]Mongoose findOneAndUpdate inside nested Arrays

I need to update the type, key and values in the 'fields' array if the componentId and the wfInstanceId id matches. 如果componentId和wfInstanceId id匹配,我需要更新'fields'数组中的类型,键和值。

This is the document that i need to do so. 这是我需要这样做的文件。

{
    "_id": {
        "$oid": "586b6d756937c22f207dd5af"
    },
    "wfInstanceId": "0111",
    "workflowId": "ash3",
    "folderURL": "ash3",
    "teamName": "teamName",
    "dataStream": [
        {
            "componentInstanceId": "componentInstanceId1",
            "componentId": "componentId1",
            "_id": {
                "$oid": "586b6d756937c22f207dd5b0"
            },
            "fields": [
                {
                    "type": "String",
                    "value": "value1",
                    "key": "key",
                    "_id": {
                        "$oid": "586b6d756937c22f207dd5b1"
                    }
                },
                {
                    "type": "String",
                    "value": "value2",
                    "key": "key1",
                    "_id": {
                        "$oid": "586b6d756937c22f207dd5b1"
                    }
                }
            ]
        },
        {
            "componentInstanceId": "componentInstanceId2",
            "componentId": "componentId22",
            "_id": {
                "$oid": "586b6d756937c22f207dd5b0"
            },
            "fields": [
                {
                    "type": "String",
                    "value": "value1",
                    "key": "key",
                    "_id": {
                        "$oid": "586b6d756937c22f207dd5b1"
                    }
                },
                {
                    "type": "String",
                    "value": "value2",
                    "key": "key2",
                    "_id": {
                        "$oid": "586b6d756937c22f207dd5b1"
                    }
                }
            ]
        }
    ],
    "id": "38f356f0-d196-11e6-b0b9-3956ed7f36f0",
    "__v": 0
}

I tried this like this, Also i tried $set over $push which is also doesn't work. 我这样尝试过,也尝试过$ push $ set这也是行不通的。

Model.findOneAndUpdate( {'wfInstanceId': '0111', 'dataStream.componentId': 'componentId1'}, {$push : { 'dataStream.fields.$.key' : 'sdsdds' }}, { upsert: true }, function (err, data) {
                if (err) {
                    reject(err);
                    console.log('error occured' + err);
                }
                console.info("succesfully saved");
                resolve (data);
            });

As they describe in this DOC it shouldbe working for update method that is also didn't work for me. 正如他们在本DOC中所述,它应该适用于对我也不起作用的更新方法。 Any help will be really appreciated to overcome this issue i'm facing. 对于克服我所面临的这个问题,我们将不胜感激。

As you are using $ operator it updates only the first matched array element in each document. 当您使用$运算符时,它仅更新每个文档中的第一个匹配数组元素。

As of now it is not possible in mongoose to directly update all array elements. 截至目前,猫鼬不可能直接更新所有数组元素。

You can do this in your case: 您可以根据自己的情况进行操作:

db.collection.find({'wfInstanceId': '0111', 'dataStream.componentId': 'componentId1'})
  .forEach(function (doc) {
    doc.datastream.forEach(function (datastream) {
      if (dataStream.componentId === componentId1) {
        dataStream.fields.forEach(function(fields){
         // you can also write condition for matching condition in field
           dataStream.fields.key="";
           dataStream.fields.value="";
           dataStream.fields.type="";
         }
      }
    });
    db.collection.save(doc);
  }); 

It is normal javascript code. 这是正常的javascript代码。 I think it's clearer for mongo newbies. 我认为对于mongo新手来说更清楚。

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

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