与Java相比,使用node.js在mongo中缓慢插入和更新

时间:2013-11-19 10:44:39

标签: java node.js mongodb

我正在编写独立脚本以在集合中插入大约5000条记录。我正在使用相同的5000条记录更新另一个集合。 我的问题是当我使用带有mongo的java编写它时,需要将近1.1秒才能逐个运行这两个操作 但是当我使用Node.js编写它时,插入记录大约需要1.1秒,更新操作需要10秒。

两个测试运行都在具有相同索引的相同集合上进行。 你能帮我解释为什么Node.js花了这么多时间比java

多近10倍

更新记录的Node.js版本

   var db = new Db(articleDBName , new Server(mongoIP, mongoPort, {safe: false, strict: false}, {auto_reconnect: true}),{w:0});
db.open(function(error,db){
    var d = new Date();
    var tc = d.getTime();
    if(error){
        console.log(error);
        closeDB(db,dbName);
    }else{
        for(var i in logs){
            var record = logs[i];

                try{
                    var articleId = utils.getString(record["articleId"]);

                    var query        =  {"articleId":articleId} ;
                    var count        =  record["count"];

                    var author       = utils.getString(record["author"]);
                    var articleCount = db.collection("ArticleCountTempNode");                       
                    var updateObj = { $set:{"author" : author,
                        "channel" : channel
                    },$inc:{"count":count}};    
                    articleCount.update(query,updateObj,{upsert: true, multi:false},function(error, result) {

                        if(error){
                            console.log("Error Log " + error);
                        }else{
                        }
                    });

                }catch(error){
                    console.log("Error Log " + error);

                }


        }

        closeDB(db,articleDBName);
        var dt = new Date();
        console.log("time Taken Article Count----"+(dt.getTime() - tc));
    }
});

更新记录的Java版本

    DB articleDBHistory = mongo.getDB(articleDBName);
    DBCollection articleCollHistory = articleDBHistory.getCollection(Config.ARTICLE_COUNT_COLLECTION);

    try {
        articleDBHistory.requestStart();

        DBObject query = null;
        BasicDBObject bDbObj = null;
        String articleId = null;
        long t = System.currentTimeMillis();
        for (DBObject dbo : dbl)
        {

            articleId = dbo.get("articleId").toString();
            if (articleId != null && articleId != "") {
                query = new BasicDBObject("articleId",
                        dbo.get("articleId")));
                bDbObj = new BasicDBObject("$set",
                        new BasicDBObject("author", dbo.get("author")))
                        .append("$inc", new BasicDBObject("count",
                                ((Double)Double.parseDouble(dbo.get("count").toString())).longValue()));
                articleCollHistory.update(query, bDbObj, true, false);

            }
        }
        } catch (Exception e) {
        e.printStackTrace();
    }finally{
        articleDBHistory.requestDone();
    }

0 个答案:

没有答案