2017-09-27 10 views
1

からアレイ内の複数のオブジェクトを更新し、私はこのコレクションを持っている:[OK]をマルチオブジェクトreq.body

{ 
    "_id" : ObjectId("59baa8af86e5df3984674639"), 
    "centralPath" : "C:\\Users\\konrad.sobon\\OneDrive - HOK\\GoogleDrive\\Work\\DynamoWork\\142_SeatManifestGenerator\\RVT2018\\CBUEC_HOK_IN-central.rvt", 
    "totalFamilies" : 31, 
    "unusedFamilies" : 18, 
    "oversizedFamilies" : 0, 
    "inPlaceFamilies" : 0, 
    "createdBy" : "konrad.sobon", 
    "createdOn" : ISODate("2017-09-14T20:19:09.525Z"), 
    "families" : [ 
     { 
      "name" : "Spot Elevation - Target Filled_HOK_I", 
      "size" : "1.2Mb", 
      "sizeValue" : 1200000, 
      "instances" : 0, 
      "elementId" : 6158, 
      "isFailingChecks" : true, 
      "isDeleted" : false, 
      "_id" : ObjectId("59bae43d2720015998392905"), 
      "tasks" : [], 
      "parametersCount" : 0, 
      "nestedFamilyCount" : 1, 
      "voidCount" : 0, 
      "refPlaneCount" : 2, 
      "arrayCount" : 0 
     }, 
     { 
      "name" : "Section Head - Filled_HOK_I", 
      "size" : "140kb", 
      "sizeValue" : 145760, 
      "instances" : 0, 
      "elementId" : 8762, 
      "isFailingChecks" : true, 
      "isDeleted" : false, 
      "_id" : ObjectId("59bae43d2720015998392904"), 
      "tasks" : [], 
      "parametersCount" : 0, 
      "nestedFamilyCount" : 1, 
      "voidCount" : 0, 
      "refPlaneCount" : 3, 
      "arrayCount" : 0 
     }, 
     { 
      "name" : "Railing Tag_HOK_I", 
      "size" : "244kb", 
      "sizeValue" : 249856, 
      "instances" : 0, 
      "elementId" : 12426, 
      "isFailingChecks" : true, 
      "isDeleted" : false, 
      "_id" : ObjectId("59bae43d2720015998392903"), 
      "tasks" : [], 
      "parametersCount" : 3, 
      "nestedFamilyCount" : 1, 
      "voidCount" : 0, 
      "refPlaneCount" : 2, 
      "arrayCount" : 0 
     }, 
     { 
      "name" : "Fixed", 
      "size" : "316kb", 
      "sizeValue" : 323584, 
      "instances" : 0, 
      "elementId" : 3499132, 
      "isFailingChecks" : true, 
      "isDeleted" : false, 
      "_id" : ObjectId("59bae43d27200159983928e7"), 
      "tasks" : [], 
      "parametersCount" : 4, 
      "nestedFamilyCount" : 2, 
      "voidCount" : 0, 
      "refPlaneCount" : 18, 
      "arrayCount" : 0 
     } 
    ], 
    "__v" : 0 
} 

私がやろうとしています何をすると、そのコレクション内の特定の「家族」を更新する要求を送信します。私はこのように見ているこれらの家族のオブジェクトの配列を持っている:

{"key": [ 
      { 
       "name" : "New Spot Elevation - Target Filled_HOK_I", 
       "size" : "1.2Mb", 
       "sizeValue" : 1200000, 
       "instances" : 0, 
       "elementId" : 6158, 
       "isFailingChecks" : true, 
       "isDeleted" : false, 
       "Id" : "59bae43d2720015998392905", 
       "tasks" : [], 
       "parametersCount" : 0, 
       "nestedFamilyCount" : 1, 
       "voidCount" : 0, 
       "refPlaneCount" : 2, 
       "arrayCount" : 0 
      }, 
      { 
       "name" : "New Section Head - Filled_HOK_I", 
       "size" : "140kb", 
       "sizeValue" : 145760, 
       "instances" : 0, 
       "elementId" : 8762, 
       "isFailingChecks" : true, 
       "isDeleted" : false, 
       "Id" : "59bae43d2720015998392904", 
       "tasks" : [], 
       "parametersCount" : 0, 
       "nestedFamilyCount" : 1, 
       "voidCount" : 0, 
       "refPlaneCount" : 3, 
       "arrayCount" : 0 
      } 
     ]} 

今、私は指定された家族のそれぞれを見つけて更新できるようにする必要があります。私は到来する配列(私はreq.bodyで送信しています)を反復するだけで、mongoの$ in演算子を使うことができるように、更新が必要な配列の配列を作ることができると思いました。その後、idを使って興味のあるプロパティを取得し、$ setを使ってそれらを更新することができると思った。ここでの私の試み:

module.exports.updateMultipleFamilies = function (req, res) { 
    var id = req.params.id; 
    var famIds = []; // [ObjectId] 
    var newFamilies = {}; // {"id_string" : family} 
    for(var key in req.body) { 
     if(req.body.hasOwnProperty(key)){ 
      for(var i = 0; i < req.body[key].length; i++){ 
       var family = req.body[key][i]; 
       newFamilies[family.Id] = family; 
       famIds.push(mongoose.Types.ObjectId(family.Id)); 
      } 
     } 
    } 

    Families 
     .updateMany(
      {_id: id, 'families._id': {$in: famIds}}, 
      {$set: {'name': newFamilies["current_document_id_help"].name}}, function(err, result){ 
       if(err) { 
        res 
         .status(400) 
         .json(err); 
       } else { 
        res 
         .status(202) 
         .json(result); 
       } 
      } 
     ) 
}; 

編集:

は、だから私は、bulkWriteコールとは異なるアプローチを試してみました。それは私にエラーを与えるものではありませんが、何も更新しません。なぜどんなアイデア?

module.exports.updateMultipleFamilies1 = function (req, res) { 
    var id = req.params.id; 
    var bulkOps = []; 

    for(var key in req.body) { 
     if(req.body.hasOwnProperty(key)){ 
      bulkOps = req.body[key].map(function(item){ 
       return { 
        'updateOne': { 
         'filter': {'_id': id, 'families._id': mongoose.Types.ObjectId(item.Id)}, 
         'update': {'$set': {'families.$.name': item.name}}, 
         'upsert': false 
        } 
       } 
      }) 
     } 
    } 

    Families.collection 
     .bulkWrite(
      bulkOps, 
      {'ordered': true, w:1}, function(err, result){ 
      if(err) { 
       res 
        .status(400) 
        .json(err); 
      } else { 
       res 
        .status(202) 
        .json(result); 
      } 
     }) 

}; 
+0

ここでチェックアウト、この答えを:https://stackoverflow.com/questions/40904007/mongodb-update-multiple-subdocuments-with-or-query - あなたはすべてのサブを更新する必要がありますあなたの上のループで個別に文書化する、私は恐れています、あなたは現在constrですあなたの 'newFamilies'配列を読み込みます。 – dnickless

答えて

-1

ファミリの配列を取得し、アプリケーションのコードを変更してファミリの配列を再度更新する必要があります。 またはあなたが行う試すことができます{$セット:{ '家族$名前を。。':newFamilies [ "current_document_id_help"]名前} 。} それは文句を言わない、すべてのあなたの家族を更新するかどうかわからないイム

最初のソリューション:

var id = req.params.id; 
 
    yourCollation.findById(id,function(err, doc){ 
 
     
 
     if(req.body.key && req.body.key.constructor === Array){ 
 
      for(var i = 0; i < req.body.key.length; i++){ 
 
      for(var x in doc.families){ 
 
       if(family.id === doc.families[x].id){ 
 
        doc.families[x] = family; 
 
       } 
 
      } 
 
      } 
 
     } 
 
    
 
    
 
    doc.save() 
 
    })

+0

これは私には意味がありません。私はあなたが意味するものを理解することができますいくつかのコードを共有するのを忘れないでください? – konrad

+0

答えを更新しました。このコードを試してください。あなたはあなたの照合名を言わなかったので、あなたの照合を変更してください –