Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

219
Views
How do I validated array of objects using mongodb validator?

I have been trying to validate my data using the validators provided by MongoDB but I have run into a problem. Here is a simple user document which I am inserting.

{
    "name"    : "foo",
    "surname" : "bar",
    "books"   : [
      {
        "name" : "ABC",
        "no"   : 19
      },
      {
        "name" : "DEF",
        "no"   : 64
      },
      {
        "name" : "GHI",
        "no" : 245
      }
    ]
}

Now, this is the validator which has been applied for the user collection. But this is now working for the books array which I am inserting along with the document. I want to check the elements inside the object which are the members of books array. The schema of the object won't change.

db.runCommand({
  collMod: "users",
  validator: {
    $or : [
      { "name"       : { $type : "string" }},
      { "surname"    : { $type : "string" }},
      { "books.name" : { $type : "string" }},
      { "books.no"   : { $type : "number" }}
    ],
  validationLevel: "strict"
});

I know that this validator is for member objects and not for array, but then how do I validate such an object ?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

It has been very long since this question was asked.
Anyways, if at all anyone comes through this.

For MongoDB 3.6 and greater version, this can be achieved using the validator.


 db.createCollection("users", {
    validator: {
       $jsonSchema: {
          bsonType: "object",
          required: ["name","surname","books"],
          properties: {
            name: {
                bsonType: "string",
                description: "must be a string and is required"
             },    
             surname: {
                bsonType: "string",
                description: "must be a string and is required"
             },         
             books: {
                bsonType: [ "array" ],
                items: {
                    bsonType: "object",
                    required:["name","no"],
                    properties:{
                        name:{
                            bsonType: "string",
                            description: "must be a string and is required"
                        },
                        no:{
                            bsonType: "number",
                            description: "must be a number and is required"
                        }
                    }
                },
                description: "must be a array of objects containing name and no"
             }
          }
       }
    }
 })

This one handles all your requirements.
For more information, refer this link

over 4 years ago · Santiago Trujillo Report

0

You can do it in 3.6 using $jsonSchema expression.

JsonSchema allows defining a field as an array and specifying schema constraints for all elements as well as specific constraints for individual array elements.

This blog post has a number of examples which will help you figure out the syntax.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!