arraysmongodbmongoosedocument

I can't send a document to my MongoDB Collection because it tell me Invalidate Document


I have a problem at moment when I try to insert a new document in my schema of MongoDB where I specified requires label and properties label, I don't know whether "Document Failed Validation" message occurs because I set the pattern label or additionalProperties label within email property. I think this is correct because It's according with my schema.

use("flow_store")
db.createCollection("people",
{
    validator:
    {
        $jsonSchema:{
            bsonType:"object",
            required:["name","age","email","type"],
            properties:
            {
                name:
                {
                    bsonType:"string",
                    description:"It must be a string and is required"
                },
                age:
                {
                    bsonType:"int",
                    description:"It must be a integer and is required"
                },
                email:
                {
                    bsonType:"string",
                    description:"It must be an email, according with a regular expression and is required",
                    pattern:"^[a-zA-Z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Z|a-z]{2,}$"
                },
                type:
                {
                    bsonType:"string",
                    enum:["employee","customer"],
                    description:"must be either 'employee' or 'customer"
                },
                stores: {
                    bsonType: "array",
                    items: {
                      bsonType: "objectId",
                    },
                    description: "must be an array of ObjectIds",
                },
                phone:
                {
                    bsonType:"string",
                    description:"It must be a phone number and is not required"
                },
                address:
                {
                    bsonType:'object',
                    required:["city","state","street"],
                    properties:
                    {
                        street:
                        {
                            bsonType:"string",
                        },
                        city:
                        {
                            bsonType:"string"
                        },
                        state:
                        {
                            bsonType:"string"
                        },
                        zip:
                        {
                            bsonType:"string"
                        }, 
                    },
                    additionalProperties:false
                },
                accountBalance:
                {
                    bsonType:"number",
                },
                salary:
                {
                    bsonType:"number"
                },
                position:
                {
                    bsonType:"string"
                },
                workStores:
                {
                    bsonType:'array',
                    items:
                    {
                        bsonType:'objectId'
                    }
                }   
            },
            additionalProperties:false
        }
    }
})

¿Do you see something missing or failing in my document to be inserted?

use("flow_store");
db.people.insertOne({
   name: "John Doe",
   age: Int32(30),
   email: "john.doe@example.com",
   type: "customer",
   stores: [],
   address: {
      street: "123 Main St",
      city: "Cityville",
      state: "CA",
      zip: "12345"
   },
   accountBalance: Number(1000.50)
}); 

I want to get a solution for my issue, I'll thank you very much


Solution

  • Yes the problem is with your additionalProperties:false setting because:

    When you specify additionalProperties: false in your JSON schema, MongoDB rejects documents that contain fields not included in your schema's properties object. Because all objects contain an automatically-generated _id field, when you set additionalProperties: false, you must include the _id field in your properties object. If you don't, all documents are rejected.

    If you want to keep this setting you need to manually add an _id to your schema properties e.g:

    "_id": { 
       "bsonType": "objectId" 
    },