标签: mongodb-schema

如何从打字稿界面生成 MongoDB JSON 模式?

我的目标是提高 MongoDB 数据库中的数据质量 - 通过使用我的目标是通过使用JSON 模式验证。我们在项目中使用打字稿,并为我们所有的集合提供接口。

\n

所以我基本上正在寻找一种有效的方法;

\n

转换此接口

\n
import { ObjectId } from \'mongodb\';\n\nexport interface Category {\n  _id: ObjectId;\n  date: Date;\n  level: string |\xc2\xa0null;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

进入这个 JSON 模式

\n
export 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}\n
Run Code Online (Sandbox Code Playgroud)\n

mongodb typescript mongodb-schema

5
推荐指数
1
解决办法
1399
查看次数

Node.js mongoose find() 方法使用对象中的键值

我想使用 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获取数据

怎么做?

谢谢

mongoose mongodb node.js mongodb-schema

1
推荐指数
1
解决办法
8121
查看次数

标签 统计

mongodb ×2

mongodb-schema ×2

mongoose ×1

node.js ×1

typescript ×1