2016-10-11 7 views
1

updateIdsの配列に基づいてn個のドキュメントを更新したいとします。私はMongoose Stream .on( 'data、function(){})を使用しています。どちらが適切なドキュメントを適切に見つけるか。しかし、今はどのように各文書を更新するのか分かりません。 on(data、function(doc){})の中にdoc.update({query}、{key:value})のようなものを書くことはできますか?マングースストリーム、どのように各文書を更新しますか?

Wine.find({ 
    '_id': { $in: updateIds} 
}).stream() 
    .on('data', function(doc){ 
     // how do I update a doc property here? 
    }) 
    .on('error', function(error) { 
     throw error; 
    }) 
    .on('end', function() { 
     // final callback 
    }); 

答えて

2

このようにすることができます。

Wine.find({ 
    '_id': { $in: updateIds} 
}).stream() 
    .on('data', function(doc){ 
     doc.set('property_name', "value"); 
     doc.save(function(err){ 
    }); 
}) 
.on('error', function(error) { 
    throw error; 
}) 
.on('end', function() { 
    // final callback 
}); 

それとも、同じデータを持つドキュメントを更新したい場合は、あなたがこれを行うことができ、

Wine.update({ _id: { $in: updateIds} }, 
    { property: "value" }, 
    { multi : true}, 
    function(err, count){ 
    }); 
+0

まあ、それは理にかなっています。どうも! – tonejac

関連する問題