【问题标题】:Node/MongoJS and bulk operationsNode/MongoJS 和批量操作
【发布时间】:2014-10-06 00:34:39
【问题描述】:

我有一个 MongoDB 2.6 数据库。 在 mongo shell 中,我可以按预期执行批量操作,并且还可以使用 Mongo 1.4.9 节点驱动程序:

// bulktest-mongodb.js:  works fine
var Db = require('mongodb').Db;
var Server = require('mongodb').Server;
var db = new Db('MyDB', new Server('localhost', 27017));

db.open(function(err, db) {
    var col = db.collection("MyCollection");

    var bulk = col.initializeUnorderedBulkOp();
    bulk.find({ _id: 1 }).update({ $inc: { bulk: 1 }});
    bulk.find({ _id: 2 }).update({ $inc: { bulk: 2 }});
    bulk.execute(function(err, result) {
        console.log("RESULT: "+JSON.stringify(result));
    });
});

RESULT: {"ok":1, ...}

但是,在我的项目中,我使用的是 MongoJS 0.14.1,它似乎是 MongoDB 1.4.9 节点驱动程序的包装器。如果我尝试使用这个 MongoJS 包装器做同样的事情,我会得到:

// bulktest-mongojs.js: does not work???
var mongojs = require("mongojs");
var db = mongojs.connect("mongodb://localhost/MyDB");
var col = db.collection("MyCollection");

var bulk = col.initializeUnorderedBulkOp();
bulk.find({ _id: 1}).update({ $inc: { bulk: 1}});
bulk.find({ _id: 2}).update({ $inc: { bulk: 2}});
bulk.execute(function(err, result) {
    console.log("RESULT: "+JSON.stringify(result));
});

> node ./bulktest-mongojs.js
...
var bulk = col.initializeUnorderedBulkOp();
               ^
TypeError: Object MyDB.MyCollection has no method 'initializeUnorderedBulkOp'

显然该方法没有被包装器公开,而它存在于驱动程序中。在 MongoJS 文档和 Google 中搜索时,我找不到与结合 MongoJS 驱动程序的批量操作相关的任何内容。我觉得这很奇怪,因为批量操作从 5 月左右就开始了。

谁能告诉我如何使用 MongoJS 驱动程序执行 Mongo 批量操作? 完全支持吗?我的实现有问题吗?

谢谢!

【问题讨论】:

    标签: mongodb mongojs


    【解决方案1】:

    这个 MongoJS 驱动程序显然还不支持批量更新。 MongoJS v0.15 刚刚发布,支持批量更新。上面的示例在这个更新的 MongoJS 驱动程序中运行良好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-18
      • 2015-09-27
      • 2012-12-11
      • 2017-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-06
      相关资源
      最近更新 更多