2016-09-07 27 views
2
id: { type: String, required: true, unique: true, default: uuid.v1 }, 
    description: { type: String }, 
    period: [{ 
     id: { type: String, default: uuid.v1 }, 
     start: { type: Date, default: Date.now }, 
     due: { type: Date }, 
     dueWarnByHours: { type: Number, integer: true }, 
     newnessByHours: { type: Number, integer: true }, 
    }], 

私はこのような埋め込みmongodbデータベース文書を持っています。私は1つマングース更新の埋め込み文書

WorkItem.update({ description: req.body.description},{period.rank: 3}, function(err, req) { 
     if (err) return console.error(err); 
     console.dir(reqWorkItemId + "Successfully removed the workItem from the database"); 
    }); 

下のようにそれを更新しようとしたが、マングース

+1

のために働く 'クエリでのみフィールドをdescription'ていますか?もしあなたが持っていれば、あなたのアップデートに[ポジション演算子 '$'](https://docs.mongodb.com/manual/reference/operator/update/positional/#update-documents-in-an-array)を使うことができますピリオド配列は、クエリ文書の一部として、つまり、 'WorkItem.update({" description ":req.body.description、" period.rank ":{" $ ne ":3}、{" $ set " $。ランク:3}}コールバック); – chridam

答えて

2

を使用して埋め込まれた子部品期>ランクを更新する方法を動作していない次のコードがお手伝いします: -

var findQuery = { description: req.body.description, 'period.id' : someId}; 
WorkItem.update(findQuery,{$set:{'period.$.rank': 3}}, function(err, req) { 
    if (err) return console.error(err); 
    console.dir(reqWorkItemId + "Successfully removed the workItem from the database"); 
}); 

OR

WorkItem.update(findQuery,{$set:{'period.$.rank': 3}}, function(err, req) { 
    if (err) return console.error(err); 
    console.dir(reqWorkItemId + "Successfully removed the workItem from the database"); 
}); 

注: - これが唯一のモミを更新します周期配列のオブジェクト。

+0

私はこれを試してみました – kohli

+0

期間配列には複数のオブジェクトまたは1つのオブジェクトしかありませんか? – Shrabanee

+0

検索クエリに追加する必要があるような、あなたの質問に対するコメントを見て、それを動作させます。 – Shrabanee

2
WorkItem.update({ id: d }, { description: req.body.description, $set: { 'status.0.rank': req.body.status.rank } }, 
     function(err, numRowsAffected, raw) { 
      if (err) return console.error(err); 
      if (numRowsAffected > 0) { 
       console.dir("reqWorkItemId" + "Successfully removed the workItem from the database"); 
      } else { 
       console.log("fail"); 
       //res.send(500, { error: 'carrier not updated' }); 
      } 
     }); 

この1つは私

関連する問題