2017-01-27 5 views
0

私はmongoとmongooseを使って、既存のドキュメントにファイルされた配列を更新しています。ここで

が私の元の文書

私は2つのフィールドを更新する必要が
{ 
    device_id : 'abc', 
    is_online : false 
} 

、1は'is_online'フィールドであり、もう一つは、配列フィールドで'actuators'フィールド、次のとおりです。

var update = {}; 
update.is_online = false; 
update.actuators = [ 
    { 
     'port': 1, 
     value: 0, 
     'type': 'binary', 
     description: 'button 0' 
    }, 
    { 
     'port': 2, 
     value: 0, 
     'type': 'binary', 
     description: 'button 1' 
    } 
]; 

DeviceModel.findOneAndUpdate(
    { device_id: device_id }, 
    { $set: update }, 
    { new: false, upsert: true }, 
    function(err, doc) { 

    } 
) 

しかし、この更新プログラムのみ既存のフィールド 'is_online'は新しい配列フィールドを挿入しません。

何が問題ですか?

+0

「スキーマ」を共有してください。 –

答えて

0
you have to push the values to array instead $set 
DeviceModel.update({device_id: device_id},{$pushAll: {actuators:update.actuators}},{upsert:true},function(err){ 
     if(err){ 
       console.log(err); 
     }else{ 
       console.log("Successfully added"); 
     } 
}); 
関連する問題