admin管理员组

文章数量:1415467

I am trying to create a collection with validator in MongoDB and faced a strange error.

This is what I'm trying to do in controller:

import { orderValidator } from 'db/validator'

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

This is content of the validator file:

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' },
          pleted_at: { bsonType: 'date' },
          paid_at: { bsonType: 'date' },
          rejected_at: { bsonType: 'date' },
          refunded_at: { bsonType: 'date' },
        }
      },

    created_at: { bsonType: 'date' },

    updated_at: { bsonType: 'date' }
  }
}

And I'm getting an error message like this:

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_mons.js:182:23) {
  ok: 0,
  errmsg: "BSON field 'create.bsonType' is an unknown field.",
  code: 40415,
  codeName: 'Location40415',
  name: 'MongoError',
  status: 500,
  [Symbol(mongoErrorContextSymbol)]: {}
}

I've searched but found any solutions related to it. What is the reason?

I am trying to create a collection with validator in MongoDB and faced a strange error.

This is what I'm trying to do in controller:

import { orderValidator } from 'db/validator'

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

This is content of the validator file:

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' },
          pleted_at: { bsonType: 'date' },
          paid_at: { bsonType: 'date' },
          rejected_at: { bsonType: 'date' },
          refunded_at: { bsonType: 'date' },
        }
      },

    created_at: { bsonType: 'date' },

    updated_at: { bsonType: 'date' }
  }
}

And I'm getting an error message like this:

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_mons.js:182:23) {
  ok: 0,
  errmsg: "BSON field 'create.bsonType' is an unknown field.",
  code: 40415,
  codeName: 'Location40415',
  name: 'MongoError',
  status: 500,
  [Symbol(mongoErrorContextSymbol)]: {}
}

I've searched but found any solutions related to it. What is the reason?

Share Improve this question edited Mar 24, 2020 at 6:01 glinda93 asked Jan 9, 2020 at 8:39 glinda93glinda93 8,4996 gold badges50 silver badges87 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

I didn't nest the object under jsonSchema. Changed the validation file like:

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

And it finally worked.

Update

Since this question and answer is gaining some traffic, I'm improving my answer to help future readers.

When do you get this error

MongoError BSON field 'create.bsonType' is an unknown field( code number 40415 ) is triggered when there is a syntax error in Mongo shell mand or Node.js script that creates a collection with validator.

If you didn't nest validator specification object correctly, you will get this error.

What the solution is

Do check if you nested validator object correctly in Mongo shell mand or Node.js code

The correct syntax is llike the belowing (for both Node.js driver and MongoDB shell):

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

本文标签: javascriptMongoDB 40415 BSON field 39createbsonType39 is an unknown fieldStack Overflow