我的目标是提高 MongoDB 数据库中的数据质量 - 通过使用我的目标是通过使用JSON 模式验证。我们在项目中使用打字稿,并为我们所有的集合提供接口。
\n所以我基本上正在寻找一种有效的方法;
\n转换此接口:
\nimport { ObjectId } from \'mongodb\';\n\nexport interface Category {\n _id: ObjectId;\n date: Date;\n level: string |\xc2\xa0null;\n}\nRun Code Online (Sandbox Code Playgroud)\n进入这个 JSON 模式
\nexport const CategoryJSONSchema = {\n required: [\'_id\', \'date\', \'level\'],\n additionalProperties: false,\n properties: {\n _id: { bsonType: \'objectId\' },\n date: { bsonType: \'date\' },\n level: { oneOf: [{ bsonType: \'null\' }, { bsonType: \'string\' }] }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n 我想使用 find() 函数从 mongodb 查找数据这是我的架构
const newcontractSchema = mongoose.Schema({
creator: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: false,
index: true,
},
status: {type: String, required: false, default: 'init'},
a_status: {type: String, required: false, default: 'pending'},
contractInfo: {
startDate: {type: Date},
endDate: {type: Date},
expectedUsage: {type: Number, default: 2000},
provider: {type: Object},
rate: {type: Number},
term: {type: Number},
userid: {type: String}
}, {timestamps: true});
Run Code Online (Sandbox Code Playgroud)
我尝试的是
Newcontract.find({contractInfo: {userid: {$eq: req.body.userid}}})
Run Code Online (Sandbox Code Playgroud)
但我无法根据contractInfo对象中的这个userid获取数据
怎么做?
谢谢