【问题标题】:MongoDB 40415: BSON field 'create.bsonType' is an unknown fieldMongoDB 40415:BSON 字段“create.bsonType”是未知字段
【发布时间】:2020-01-09 08:39:14
【问题描述】:

我正在尝试在 MongoDB 中使用验证器创建一个集合,但遇到了一个奇怪的错误。

这就是我要在控制器中做的事情:

import { orderValidator } from 'db/validator'

await db.createCollection('orders', orderValidator)
console.log('collection orders created')

这是验证器文件的内容:

module.exports = {
  bsonType: 'object',
  required: ['buyer_id', 'seller_id', 'insta_id', 'time', 'posts', 'price', 'total'],
  properties: {

    buyer_id: { bsonType: 'objectId' },

    seller_id : { bsonType: 'objectId' },

    insta_id: { bsonType: 'objectId' },

    category: {
        bsonType: 'string',
        maxLength: 1000
      },

    with_bio : { bsonType: 'bool' },

    bio_url: {
        bsonType: 'string',
        maxLength: 65535
      },

    swipe_up_url: {
        bsonType: 'string',
        maxLength: 65535
      },

    start_from: { bsonType: 'date' },

    caption: {
        bsonType: 'string',
        maxLength: 65535
      },

    additional_info: {
        bsonType: 'string',
        maxLength: 65535
      },

    posts: {
        bsonType: 'array',
        minItems: 1,
        maxItems: 100,
        items: {
          bsonType: 'string',
          maxLength: 65535
        }
      },

    time: {
        bsonType: 'int',
        minimum: 0
      },

    price: {
        bsonType: 'double',
        minimum: 0
      },

    bio_price: {
        bsonType: 'double',
        minimum: 0
      },

    charge: {
        bsonType: 'double',
        minimum: 0
      },

    total: {
        bsonType: 'double',
        minimum: 0
      },

    history: {
        bsonType: 'object',
        properties: {
          created_at: { bsonType: 'date' },
          accepted_at: { bsonType: 'date' },
          started_at: { bsonType: 'date' },
          completed_at: { bsonType: 'date' },
          paid_at: { bsonType: 'date' },
          rejected_at: { bsonType: 'date' },
          refunded_at: { bsonType: 'date' },
        }
      },

    created_at: { bsonType: 'date' },

    updated_at: { bsonType: 'date' }
  }
}

我收到如下错误消息:

collection orders not exists
MongoError: BSON field 'create.bsonType' is an unknown field.
    at Connection.<anonymous> ({ProjectDir}\backend\node_modules\mongodb\lib\core\connection\pool.js:466:61)
    at Connection.emit (events.js:210:5)
    at processMessage ({ProjectDir}\backend\node_modules\mongodb\lib\core\connection\connection.js:384:10)
    at Socket.<anonymous> ({ProjectDir}\backend\node_modules\mongodb\lib\core\connection\connection.js:553:15)
    at Socket.emit (events.js:210:5)
    at addChunk (_stream_readable.js:309:12)
    at readableAddChunk (_stream_readable.js:290:11)
    at Socket.Readable.push (_stream_readable.js:224:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:182:23) {
  ok: 0,
  errmsg: "BSON field 'create.bsonType' is an unknown field.",
  code: 40415,
  codeName: 'Location40415',
  name: 'MongoError',
  status: 500,
  [Symbol(mongoErrorContextSymbol)]: {}
}

我已经搜索过,但找到了与之相关的任何解决方案。是什么原因?

【问题讨论】:

    标签: javascript node.js mongodb adonis.js


    【解决方案1】:

    我没有将对象嵌套在 jsonSchema 下。 更改了验证文件,如:

    module.exports = {
      validator :{
        $jsonSchema : {
          bsonType: 'object',
          required: ['buyer_id', 'seller_id', 'insta_id', 'time', 'posts', 'price', 'total'],
          //... rest omitted
        }
      }
    }
    

    它终于奏效了。

    更新

    由于这个问答获得了一些流量,我正在改进我的答案以帮助未来的读者。

    什么时候出现这个错误

    MongoError BSON field 'create.bsonType' is an unknown field( 代码号 40415 ) 在 使用验证器创建集合的 Mongo shell 命令或 Node.js 脚本中存在语法错误时触发。

    如果你没有正确嵌套验证器规范对象,你会得到这个错误。

    解决办法是什么

    请检查您是否在 Mongo shell 命令或 Node.js 代码中正确嵌套了验证器对象

    正确的语法如下所示(对于 Node.js 驱动程序和 MongoDB shell):

    db.createCollection('your_collection', {
      validator: {
        $jsonSchema: {
          bsonType: 'object',
          required: ['property_1', 'property_2'],
          properties: {
            property_1: {
              bsonType: 'string'
            },
            property_2: {
              bsonType: 'string'
            }
          }
        }
      }
    });
    

    【讨论】:

      猜你喜欢
      • 2021-12-31
      • 2018-06-08
      • 2022-07-01
      • 2017-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-18
      相关资源
      最近更新 更多