javascript - Insert Array in existing Document -
i have document this:
_id: objectid("559c1d2ad8291bc9368b4568") tablename: "iweo_iwbb" out_user: "pb" out_email: "email" out_date: "15.05.2015" and want add array this:
"inventar": [ { "ean": "2", "name": "name2", "runtime": "0", "art": "null", "marker": "null", "stammkost": "null", "accepted": "0" }, { "ean": "1", "name": "name1", "runtime": "0", "art": "null", "marker": "null", "stammkost": "null", "accepted": "0" } ], in old php-server used code below insert it. right command "update". in node.js seems command.
foreach($jarray $value){ //$uuid = uniqid('', true); $tablename = $value['tablename']; $ean = $value["ean"]; $runtime = $value["runtime"]; $art = $value["art"]; $marker = $value["marker"]; $stammkost = $value["stammkost"]; $new_data = array( //array ( 'ean' => $ean, 'runtime' => $runtime, 'art' => $art, 'marker' => $marker, 'stammkost' => $stammkost, 'accepted' => '0' //) ); try { $collection->update(array("tablename"=>$tablename),array('$push' => array("inventar" => $new_data))); echo json_encode($collection); } catch ( mongoconnectionexception $e ) { echo '<p>update failed</p>'; exit(); } } in new node.js use code below:
tables.foreach(function(table) { var tablename = table.tablename; var name = table.name ; var ean = table.ean; var runtime= table.runtime; var art = table.art; var marker = table.marker; var stammkost = table.stammkost; console.log(tablename+" "+ean+" "+name+" "+runtime+" "+art+" "+marker+" "+stammkost); outaccept.update(function (err, data) { if (err) console.log(err); else { console.log(data); } }); response.end(); //} }); }); the output in console is:
iweo_iwbb_01062015 1 name1 11337 null null { ok: 0, n: 0, nmodified: 0 } iweo_iwbb_01062015 2 name2 null null { ok: 0, n: 0, nmodified: 0 } why isnt updated/inserted? wrong command?
there few things wrong in code here. first , foremost note running in "async" environment , need change thinking on how things.
your previous php code "blocking", means every line of code must complete before moving on next line of code. includes waiting database server perform update , return response.
you cannot use basic control loops functions inside them perform asynchronously. instead need can call next iteration of loop (or @ least signal single iteration complete ) once asynchronous function "update" has returned result.
the second point here "nothing updated" because did not tell function update or update matched document with.
the following analogous original php listing, adjusted "async" methods use async.eachseries loop control async library:
async.eachseries( tables, function(table,callback) { var tablename = table.tablename; delete table.tablename; // remove key rather re-construct outaccept.update( { "tablename": tablename }, { "$push": { "inventar": table } }, function(err,numaffected) { console.log( numafftected ); // tells how many updated or nothing callback(err) } ); }, function(err) { // comes here on completion of array items } ); the .findoneandupdate() command instead returns document modified , modifications if ask them { "new": true }
async.eachseries( tables, function(table,callback) { var tablename = table.tablename; delete table.tablename; outaccept.findoneandupdate( { "tablename": tablename }, { "$push": { "inventar": table } }, { "new": true }, function(err,doc) { console.log( doc ); // shows modified document callback(err) } ); }, function(err) { // comes here on completion of array items } ); if want add multiple array elements @ once, or if have single element directly in array use $each modifier $push:
var inventor = [ { "ean": "2", "name": "name2", "runtime": "0", "art": "null", "marker": "null", "stammkost": "null", "accepted": "0" }, { "ean": "1", "name": "name1", "runtime": "0", "art": "null", "marker": "null", "stammkost": "null", "accepted": "0" } ]; outaccept.update( { "tablename": tablename }, { "$push": { "inventar": { "$each": inventar } } }, function(err,numaffected) { // work in here } );
Comments
Post a Comment