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' },
completed_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_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)]: {}
}
I've searched but found any solutions related to it. What is the reason?