【问题标题】:Can't create MongoDb connection with 2.2.10 Node.js driver无法使用 2.2.10 Node.js 驱动程序创建 MongoDb 连接
【发布时间】:2017-02-25 10:49:26
【问题描述】:

尝试打开连接(使用mongodb本机驱动程序2.2.10和mongoose4.6.3)。

获取异常:

must pass in valid bson parser

看起来这是connection.js55

出现同样的错误:

1. for `mongodb` client fails on `MongoClient.connect`.
2. for `mongoose` it fails on startup (before any code execution).

根据文档,我不需要将任何值传递给 bson 选项字段。

【问题讨论】:

  • 我认为这可能与错误的连接 url 有关,你能告诉你的连接 url 是怎样的吗? (省略机密信息,只想看格式)

标签: node.js mongodb typescript mongoose


【解决方案1】:

我得到同样的错误。似乎从 v2.2.0 开始,它发生在您不关闭连接时,即使 Promise 中返回的值也是如此。

来自文档updateOne 方法(关注db.close()):

// Example of a simple updateOne operation using a Promise.

var MongoClient = require('mongodb').MongoClient,
  test = require('assert');
MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
  // Get the collection
  var col = db.collection('update_one_with_promise');
  col.updateOne({a:1}
    , {$set: {a:2}}
    , {upsert:true}).then(function(r) {
    test.equal(1, r.matchedCount);
    test.equal(1, r.upsertedCount);
    // Finish up test
    db.close();
  });
});

我不知道为什么对 Promise 也通过 db 感到非常不舒服。 我回滚到 v2.1.21

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-13
    • 2018-05-24
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 2020-10-29
    • 2017-06-23
    相关资源
    最近更新 更多