简体   繁体   English

编写有关在node.js的mongodb本机驱动程序中插入文档的问题

[英]Write concerns inserting document in mongodb native driver for node.js

I'm having problems using write concerns with mongodb native driver for Node.js. 我在对Node.js的mongodb本机驱动程序使用写问题时遇到问题。 I'm using a single MongoDB server in localhost. 我在本地主机中使用单个MongoDB服务器。 This is the code i'm using: 这是我正在使用的代码:

function insertNewDoc(newdoc, cbsuccess, cberror){

db.collection('test').insert(newdoc, {w: 1, wtimeout: 2000}, function(err){
if (err) cberror(err);
else cbsuccess(newdoc);
});

}

I've tried to stop mongodb just before executing this function, but it keeps trying until mongo is on again, and then it inserts the document. 我试图在执行此功能之前停止mongodb,但它一直尝试直到mongo重新打开,然后再插入文档。 What I want is to set a timeout so in case the document had not been successfully inserted after 2 seconds, it returns me an error. 我想要的是设置一个超时,以便万一2秒后未成功插入文档,它会向我返回错误。

wtimeout is about timeout for waiting for write concern to return, not about the actual connection or insert. wtimeout与等待写关注返回的超时有关,与实际的连接或插入无关。 Also connections are established before the insert() . 连接也要在insert()之前建立。

By default node-mongodb-native driver will queue up operations until the database is back again. 默认情况下,node-mongodb-native驱动程序将使操作排队,直到再次恢复数据库为止。

If you want to disable this buffer set bufferMaxEntries to 0 . 如果要禁用此缓冲区,请将bufferMaxEntries设置为0
See here for more information. 有关更多信息,请参见此处

See this example code: 请参见以下示例代码:

// node-mongodb-native_test-connection.js file

var MongoClient = require('mongodb').MongoClient;

// Connect to the db
MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
  if(err) {
    return console.dir(err);
  }
  console.log(new Date() + ' - We are connected');
  // db.close();
  db.on('close', function () {
    console.log(new Date() + ' - Error...close');
  });

  var collection = db.collection('test');

  setTimeout(function() {
    console.log(new Date() + ' - trying to insert');
    collection.insert({'newdoc': 1}, {w: 1, wtimeout: 2000}, function(err, item){
      if(err) { return console.dir(err); }
      console.log(item);
    });
  }, 2000);
});

Output example: 输出示例:

$ node node-mongodb-native_test-connection.js
Wed Mar 12 2014 17:31:54 GMT+0000 (GMT) - We are connected
Wed Mar 12 2014 17:31:55 GMT+0000 (GMT) - Error...close
Wed Mar 12 2014 17:31:56 GMT+0000 (GMT) - trying to insert
[ { newdoc: 1, _id: 53209a0c939f0500006f6c33 } ]

And see the logs 并查看日志

Wed Mar 12 17:31:55.719 [signalProcessingThread] got signal 2 (Interrupt: 2), will terminate after current cmd ends
Wed Mar 12 17:31:55.719 [signalProcessingThread] now exiting
...
Wed Mar 12 17:31:59.215 [initandlisten] MongoDB starting : pid=67863 port=27017 dbpath=/data/db/ 64-bit host=localhost
...
Wed Mar 12 17:31:59.237 [initandlisten] waiting for connections on port 27017
Wed Mar 12 17:31:59.730 [initandlisten] connection accepted from 127.0.0.1:56730 #1 (1 connection now open)

Change the connection option like so: 更改连接选项,如下所示:

MongoClient.connect('mongodb://localhost:27017/test', {
    db: {bufferMaxEntries:0},
  },
  function(err, db) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM