MongoDB $ jsonSchema验证的正确语法

时间:2019-02-01 19:07:29

标签: node.js mongodb

我正在尝试类似于此处显示的示例:https://docs.mongodb.com/manual/reference/operator/query/jsonSchema/

使用Node.js和标准的MongoDB驱动程序(我不使用Mongoose)。

我尝试将validationAction更改为warm,将validationLevel更改为off,但是即使那样,我仍然遇到相同的错误。 我假设我弄乱了语法,下面是我的代码



// Database Name
const dbName = 'testproject';

// Connection URL 
const url = `mongodb://localhost:27017/${dbName}`;


function createStudents(db) {
    db.createCollection("students", {
        validator: {
           $jsonSchema: {
              bsonType: "object",
              required: [ "name", "year", "major", "address.city", "address.street" ],
              properties: {
                 name: {
                    bsonType: "string",
                    description: "must be a string and is required"
                 },
                 gender: {
                    bsonType: "string",
                    description: "must be a string and is not required"
                 },
                 year: {
                    bsonType: "int",
                    minimum: 2017,
                    maximum: 3017,
                    exclusiveMaximum: false,
                    description: "must be an integer in [ 2017, 3017 ] and is required"
                 },
                 major: {
                    enum: [ "Math", "English", "Computer Science", "History", null ],
                    description: "can only be one of the enum values and is required"
                 },
                 "address.city" : {
                    bsonType: "string",
                    description: "must be a string and is required"
                 },
                 "address.street" : {
                    bsonType: "string",
                    description: "must be a string and is required"
                 }
              }
           }
        }
     })
}

async function insertStudent(db){
  await db.collection('students').insertOne({
        name: "Alice",
        year: 2019,
        major: "History",
        address: {
           city: "NYC",
           street: "33rd Street"
        }
     }).catch(e => console.log(e))
}


// Use connect method to connect to the server
async function connect () {
    //Connect to the client
    const client = await MongoClient.connect(url, { useNewUrlParser: true }).catch(e => {throw new Error(400)})
    const db = client.db(dbName)
    //Create the students collection
    createStudents(db);
    //Insert a Student
    await insertStudent(db)
    const cursor = await db.collection('students').find({}).toArray();
    console.log(cursor)
}

connect();

游标始终为空,因此没有文档被添加到集合中。

我得到的错误如下:

    at Function.create (/Users/myuser/projects/Javascript/mongotest/node_modules/mongodb/node_modules/mongodb-core/lib/error.js:43:12)
    at toError (/Users/myuser/projects/Javascript/mongotest/node_modules/mongodb/lib/utils.js:149:22)
    at coll.s.topology.insert (/Users/myuser/projects/Javascript/mongotest/node_modules/mongodb/lib/operations/collection_ops.js:848:39)
    at /Users/myuser/projects/Javascript/mongotest/node_modules/mongodb/node_modules/mongodb-core/lib/connection/pool.js:532:18
    at process._tickCallback (internal/process/next_tick.js:61:11)
  driver: true,
  name: 'MongoError',
  index: 0,
  code: 121,
  errmsg: 'Document failed validation',
  [Symbol(mongoErrorContextSymbol)]: {} }


0 个答案:

没有答案