Schema validation for MongoDB

Hi All,

I wrote the below schema validator for MongoDB and the below are the insert queries for the collection named “posts”. Basically, the idea is to create a blog, which will have title, author, tags, and comments and comments will be the nested documents.
I am able to run the validator successfully but, not able to run the insert queries. The schema validation is failing.

db.users.find()
{ _id: ObjectId("647b983d65af5108a7f21679"),
  name: 'shubhra garg',
  age: 34,
  email_id: 'shubhra.garg09@gmail.com' }
{ _id: ObjectId("647b983d65af5108a7f2167a"),
  name: 'ankit garg',
  age: 29,
  email_id: 'gargankit2201@gmail.com' }
{ _id: ObjectId("647b9b0865af5108a7f2167d"),
  name: 'anita agrawal',
  age: 44,
  email_id: 'anita49@gmail.com' }
{ _id: ObjectId("647b9b0865af5108a7f2167e"),
  name: 'sp agrawal',
  age: 64,
  email_id: 'spqagrawal@gmail.com' }

db.createCollection('posts',
                          {validator: {
                              $jsonSchema:{
                                bsonType:'object',
                                required:['title','text','author','comments'],
                                properties:{
                                    title:{
                                        bsonType:'string',
                                        description:"Title is a required field"
                                    },
                                    text:{
                                        bsonType:'string',
                                        description:"Text field is required"
                                    },
                                    author:{
                                        bsonType:'objectId',
                                        description:"Author is required field"
                                    },
                                    comments:{
                                        bsonType:'array',
                                        description:"must be an array of list and is required field",
                                        items:{
                                            bsonType:'objectId',
                                            required:['text','creator'],
                                            properties:{
                                                text:{
                                                    bsonType:'string',
                                                    description:"text is a required field"
                                                },
                                                creator:{
                                                    bsonType:'objectId',
                                                    description:"Creator field is required"
                                                }
                                                
                                            }
                                        }
                                    }
                                }
                            }
                          }
                        }
                    )


Sharing two insert queries for MongDB:

db.posts.insertOne({title: "My first post",text:"Happy me!", author:ObjectId("647b983d65af5108a7f21679"), tags:["new","generation","happy"], comments:[{text:"Good to see you happy", creator:ObjectId("647b983d65af5108a7f2167a")}]})
db.posts.insertOne({title: "My first post",text:"Happy me!", author:ObjectId("647b983d65af5108a7f21679"),comments:[{text:"Good to see you happy", creator:ObjectId("647b9b0865af5108a7f2167d")}]})

@christophberger Will you be able to help on this? I am stuck.

@shubhragrag Sorry, I have to decline. I know little more about MongoDB than the name and the fact that it is a document-oriented database.

The first query contains a tags field that I don’t see in the $jsonSchema, but the second query looks fine at a first glance.

Does the failing schema validation emit any useful error messages?

@ Metalymph can you please help with this program of schema validation?