【发布时间】:2015-09-26 17:09:50
【问题描述】:
我有这样的文件:
_id: ObjectId("559c1d2ad8291bc9368b4568")
tablename: "IWEO_IWBB"
out_user: "pb"
out_email: "email"
out_date: "15.05.2015"
并想像这样添加数组:
"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"
}
],
在我的旧 PHP-Server 中,我使用下面的代码插入它。 正确的命令是“更新”。在 node.js 中,它似乎是另一个命令。
foreach($jArray as $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();
}
}
在我的新 node.js 中,我使用以下代码:
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();
//}
});
});
控制台的输出是:
IWEO_IWBB_01062015 1 name1 11337 A null null
{ ok: 0, n: 0, nModified: 0 }
IWEO_IWBB_01062015 2 name2 A null null
{ ok: 0, n: 0, nModified: 0 }
为什么没有更新/插入?是不是命令不对?
【问题讨论】:
-
那么您是否在遇到问题的 node.js 服务器上使用 PHP?
-
'$push'?这是一个包含字符$、p、u等的字符串……它不是一个名为 push 的变量。 -
抱歉,我想从 php 迁移到 node。在 php 中它的工作,在节点 Not yet
-
@MarcB 你没有阅读我的问题。 php 部分正在运行,不予讨论...
-
我想我应该使用 findOneAndUpdate gist.github.com/aheckmann/3005513
标签: javascript node.js mongodb mongodb-query