mongoDB getLastError()与javascript和node.js不起作用

Dho*_*gin 5 javascript mongodb getlasterror

我有一个使用mongoDB的node.js应用程序,并且有一个函数用于删除集合中的所有文档,然后用一些示例文档重新填充数据库中的集合。

编写新功能时,我会偶尔使用它来测试我的应用程序是否具有已知数据。

我遇到的问题是,如果我在所有集合上调用drop()然后调用一些插入来重新填充,有时记录的记录将少于我期望插入到数据库中的记录,有时整个集合将丢失。我的计数会检查所有说的#条记录,这些记录与期望的数量匹配,但是即使说它已插入,整个集合也可能丢失。

我只能假设,因为它仅发生在大约50%的时间中,也许某种程度上插入是在drop()完成之前开始插入,然后drop()会擦除插入的文档,因为它正在运行异步。

我在所有插入/更新命令上都使用了{safe:true},但是db.collection.drop()没有任何参数,而且似乎没有办法指定安全选项。

因此,我尝试在drop()之后使用db.getLastError()来阻止插入,直到拖放完成。

使用db.getLastError()或db.getlasterror()会引发错误:TypeError:对象#<Db>没有方法'getlasterror'

对getLastError的简化调用:

var mongo = require("mongodb");

  var db_conn = new mongo.Db(dbName, new mongo.Server(host, port, { auto_reconnect: true }), {});
  db_conn.open(function(err, db) {
    if(!err) {
      console.log("Database connection: \033[32mopen\033[0m");
    } else {
      console.log("Database connection: \033[31mfailed\033[0m");
    }
  });
  db_conn.getLastError();
  this.db_conn = db_conn;
  this.users = db_conn.collection("users");
  this.companies = db_conn.collection("companies");
  this.suppliers = db_conn.collection("suppliers");
Run Code Online (Sandbox Code Playgroud)

我已经在mongodb模块1.1.4和1.1.7上尝试过此操作,但得到一个错误。

我使用的参考文献有误吗?在JavaScript的代码示例中,我在stackoverflow或Web上通常找不到关于getlasterror的任何内容。

如果有更好的方法可以做到这一点,我会全力以赴。

js docs:http : //docs.mongodb.org/manual/reference/javascript/#db.getLastError

mongo文档:http : //www.mongodb.org/display/DOCS/getLastError+Command

---- 2012年9月22日更新

使用下面的答案,我可以调试出根本问题所在,并且与我期望的一样。使用默认的5个开放连接池时,会随机触发分支,然后插入。取决于它们的发送顺序和它们各自进入的池,如果一个放置快速完成,然后在该集合的放置完成之前快速进行插入,则它将在以后删除插入的记录。

我的解决方法是在drop()命令的回调函数中按链接顺序包装所有的drop函数,以便每个drop函数都在触发前一个drop的回调函数之后发生,然后最后调用一个触发所有插入的回调函数。这很麻烦,但是我现在已经用20个简单插入的基本记录集和100,000条记录的loadtest测试了initializedb函数大约25次。还没有失败。

时间会证明一切,但是这个.lastError()答案可以解决此问题。

---- 2012年9月30日更新

我想测试一个星期左右,然后再回复以确保它确实有效。上周,我已经在26个记录的基础集和100,000个记录的集中批量运行了大约100次初始化脚本。我没有看到它现在使用我的新代码失败。

我用于删除集合的代码:(我有一个DatabaseHandler对象,与原型函数一起用于操作)

DatabaseHandler.prototype.dropAll = function(callback, callbackArg) {
  // drop all the documents
  var that = this;
  var finished = callback || function() { };
  var finishedArg = callbackArg;
  this.companies.drop(function(err, reply) {
    if(reply) {
      console.log("[db] Companies collection dropped");
    } else {
      console.log("\033[31m[Error]\033[0m Companies collection failed to drop");
    }
    // drop all the user documents
    that.users.drop(function(err, reply) {
      if(reply) {
        console.log("[db] Users collection dropped");
      } else {
        console.log("\033[31m[Error]\033[0m Users collection failed to drop");
      }
      // drop all the course documents
      that.courses.drop(function(err, reply) {
        if(reply) {
          console.log("[db] Courses collection dropped");
        } else {
          console.log("\033[31m[Error]\033[0m Courses collection failed to drop");
        }
        // drop all the course purchase documents
        that.course_purchases.drop(function(err, reply) {
          if(reply) {
            console.log("[db] Course purchases collection dropped");
          } else {
            console.log("\033[31m[Error]\033[0m Course purchases collection failed to drop");
          }
          console.log("Dropped all documents and collections");
          return finished(finishedArg);
        });
      });
    });
  });
};
Run Code Online (Sandbox Code Playgroud)

然后,我有一个初始化函数,该函数带有一个参数来指定是否要额外的负载测试数据initialize(addloadtestdata)。如果我指定dropdata,它将调用dropAll并接受初始化函数进行回调。因此,在删除操作完成后,它将调用初始化回调,并仅在删除操作完成后才开始重新添加文档。我还有一个小计数功能,可以在完成插入操作后检查插入了多少文档,以验证是否插入了正确数量的文档,没有遗漏任何文档。这可能是我添加的所有功能中最好的功能,因此,每次初始化时,我都可以看到插入的文档的确切数量是否正确,或者甚至丢失了1个。

function initialize(doLoadtest){
  // do work here
}
// drop all collections and documents
if(dropdata) {
  // drop all the data first then call the initialize as a callback function when completed dropping
  Database.dropAll(initialize, includeLoadtestData);
} else {
  initialize(includeLoadtestData);
}
Run Code Online (Sandbox Code Playgroud)

希望能有所帮助

she*_*man 1

您使用的是node-mongodb-native 驱动程序吗?如果是这样,请查看 lastError 命令。

http://mongodb.github.com/node-mongodb-native/api- generated/db.html#lasterror