0

I have the below collection schema which has been created and passed the validation:

db.createCollection("Settings",
{ validator: { $or:
    [
        {cID:{ $type: "int" } },
        {sID : { $type: "int" } },
        {default :
            [
                {default1:              
                    {
                        name : { $type: "string" },
                        priority : { $type: "string" },
                        autoMoveToCompleted : { $type: "string" },
                        notifyInAdvance : { $type: "string" }
                    }
                },
                {default2 :
                    {
                        name : { $type: "string" },
                        priority : { $type: "string" },
                        autoMoveToCompleted : { $type: "string" }
                    }
                }
            ]
        },
        {applied: [
            {applied1:
                {   name : { $type: "string" },
                    priority : { $type: "string" },
                    autoMoveToCompleted : { $type: "string" },
                    notifyInAdvance : { $type: "string" } 
                }
            },
            {applied2 :
                {
                    name : { $type: "string" },
                    priority : { $type: "string" },
                    autoMoveToCompleted : { $type: "string" }
                }
            }
        ]}
    ]
}
}
)

But, I cannot generate a valid insert statement for above schema as it involves arrays. I tried to find the resolution in google but could not find anything related to prepare an insert statement similar to above schema.

Please help generating the insert statement for this schema for one mongo document, to run the insert query on mongo shell.

S2S2
  • 8,322
  • 5
  • 37
  • 65
  • I think what you are looking for is a bit "out of scope" for MongoDB schema validation. It's really only in infancy. The sort of conditions you are looking to apply are better handled in "client side logic". – Neil Lunn Jun 15 '17 at 13:54
  • Can you include an example of the document you are trying to insert and also describe your validation goals? For example: check the first two elements of the `default` or `applied` arrays (or perhaps check that these are arrays, if present?). It's also worth noting that document validation is an optional MongoDB feature and more flexible than declaring a schema that all documents must comply with - validation rules are only checked when documents are inserted or updated. It looks like you might be conflating validation with schema or model definitions you would use in your application code. – Stennie Jun 17 '17 at 02:13

1 Answers1

0

I only can propose to change validation schema like:

db.createCollection("Settings",
{ validator: { $or:
    [
        {cID:{ $type: "int" } },
        {sID : { $type: "int" } },
        {"default.default1.name" : { $type: "string" },
         "default.default1.priority" : { $type: "string" },
         "default.default1.autoMoveToCompleted" : { $type: "string" },
         "default.default1.notifyInAdvance" : { $type: "string" },
         "default.default2.name" : { $type: "string" },
         "default.default2.priority" : { $type: "string" },
         "default.default2.autoMoveToCompleted" : { $type: "string" }
        }
    ]
}
}
)

It must work

Miriam Farber
  • 18,986
  • 14
  • 61
  • 76
Dmytro
  • 66
  • 3