【发布时间】:2014-03-31 19:12:39
【问题描述】:
我有一个非常大的文件,其中包含大量 JSON 字符串(超过 100K),每行一个字符串。
我想读取每一行,将其插入数据库,在插入项目后,我想用初始插入的基本信息更新另一个数据库中的另一个文档。而且因为我是一个 nodejs 新手,所以我很难理解我做错了什么。这是我目前所拥有的。
var lineReader - require("line-reader");
lineReader.eachLine(filePath, function(line, last){
if(count == 1){
asyncAdd(JSON.parse(line));
}
})}
var counter = 0;
function asyncAdd(jsonString){
async.waterfall([
//this function calls the inserter
function(callback){
counter++;
addJson(jsonString, function(doc){
callback(null, doc);
console.log("Added " + counter);
})
},
//This function calls the indexing function
function(doc, callback){
console.log("indexing: " + counter);
updateDBIndex(doc, function(err, savedDocument){
callback(err, savedDocument);
});
}
],
function(err, results){
if(err){
return console.error("Error " + err);
}
console.log("indexed " + counter);
});
}
基本上,如果我的文件看起来像:
{"_id": "1", "item":"glove", "color": "red"}\n
{"_id": "4", "item":"hat", "color" : "red"}\n
{"_id": "6", "item":"hat","color" : "blue"}\n
我希望输出看起来像, 加了 1 索引 1 索引 1 加了 2 索引 2 索引 2 添加了 3 索引 3 索引 3
任何帮助将不胜感激!谢谢!
【问题讨论】:
-
“数据库索引”是什么意思?上面的 sn-p 输出什么?
-
数据库索引是我用来跟踪我插入的所有内容的东西。所以在我的小文件中,我上面的索引是 {"hat" : "2" "glove":"1" "red":"2" "blue":"1"。
-
您是在为此编写自己的数据库,还是我们中的一个人误解了数据库软件的工作原理?
-
@JamesB 所以“数据库索引”是指行号?
-
我的所有项目都已正确添加到数据库中,但异步性质使索引全部错误。我正在编辑以澄清我的意思。
标签: javascript node.js mongodb asynchronous