我正试图从Mongoose切换到Node.js的本机mongodb驱动程序。我试图像猫鼬一样用本机驱动程序实现架构和架构验证。我的第一个问题是,如果可以的话,我该怎么办?
我正在使用快速中间件来验证传入的请求。够了还是我需要模式级别的验证。
我知道创建架构验证的语法。
db.createCollection('posts', {
validator: {
$jsonSchema: {
bsonType: 'object',
required: ['title', 'text', 'creator', 'comments'],
properties: {
title: {
bsonType: 'string',
description: 'must be a string and is required'
},
text: {
bsonType: 'string',
description: 'must be a string and is required'
},
creator: {
bsonType: 'objectId',
description: 'must be an objectid and is required'
},
comments: {
bsonType: 'array',
description: 'must be an array and is required',
items: {
bsonType: 'object',
required: ['text', 'author'],
properties: {
text: {
bsonType: 'string',
description: 'must be a string and is required'
},
author: {
bsonType: 'objectId',
description: 'must be an objectid and is required'
}
}
}
}
}
}
}
});
但这仅适用于shell,不适用于nodejs的mongo驱动程序