Mongoose bulkWrite - 'q'的错误类型

时间:2017-04-15 13:42:56

标签: node.js mongodb mongoose bulkinsert

对于我的测试套件,我想在数据库中批量写入测试信息,然后批量删除整个测试中输入的任何测试信息,以便回到干净的平台。我这样做是通过在db上运行bulkWrite来传递通过nodeJS的require语句加载的JSON文件的内容。

问题在于数据集

[ { deleteOne: { username: 'test-author' } } ]

传递给models[key].collection.bulkWrite(action[key]),其中key是感兴趣的模型的名称,action是JSON文件,我收到以下错误:

{ MongoError: Wrong type for 'q'. Expected a object, got a null.
    at Function.MongoError.create (/var/www/website/server/node_modules/mongodb-core/lib/error.js:31:11)
    at /var/www/website/server/node_modules/mongodb-core/lib/connection/pool.js:483:72
    at authenticateStragglers (/var/www/website/server/node_modules/mongodb-core/lib/connection/pool.js:429:16)
    at Connection.messageHandler (/var/www/website/server/node_modules/mongodb-core/lib/connection/pool.js:463:5)
    at Socket.<anonymous> (/var/www/website/server/node_modules/mongodb-core/lib/connection/connection.js:339:20)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:176:18)
    at Socket.Readable.push (_stream_readable.js:134:10)
    at TCP.onread (net.js:548:20)
  name: 'MongoError',
  message: 'Wrong type for \'q\'. Expected a object, got a null.',
  ok: 0,
  errmsg: 'Wrong type for \'q\'. Expected a object, got a null.',
  code: 14,
  codeName: 'TypeMismatch' }

我做了一些研究,但无法找到解决这个问题的方法。错误本身是毫无意义的,所以我无法理解它。知道如何解决问题吗?

任何帮助将不胜感激! 干杯!

1 个答案:

答案 0 :(得分:2)

根据MongoDB APIdeleteOnedeleteManyupdateOneupdateManyreplaceOnereplaceMany操作需要具有属性filter,该属性充当查询的过滤器。

但是Mongoose's API显示以下(错误的)示例:

Character.bulkWrite([
  ...
  {
    deleteOne: {
      { name: 'Eddard Stark' }
    }
  }
]).then(handleResult);

因此,发送的数据来自:

[{
  "deleteOne": { "username": "test-author" }
}]

[{
  "deleteOne": { "filter": { "username": "test-author" }} 
}]

我会确保将消息传递给mongoosejs开发组。