-
Type: New Feature
-
Resolution: Unresolved
-
Priority: Major - P3
-
None
-
Affects Version/s: None
-
Component/s: Querying
-
Query Execution
-
(copied to CRM)
This feature would allow users to set default values for fields within in a collection. If the field is not present in the document inserted, the field would be set to the default value.
A possible implementation could look like this:
> db.createCollection( "coll", { "validator": { "a": { "$type": "string" }, "b": { "$type": "string" }, "c": { "$type": "string" }}} {"fieldValueDefaults": { "c": "defaultString"}} ) { "ok" : 1 } > db.coll.insert({ "a": "A", "b": "B" }) WriteResult({ "nInserted" : 1 }) > db.coll.findOne() { "_id" : ObjectId("575721a2dc744047e1566f0f"), "a" : "A", "b" : "B", "c" : "defaultString" }
Original summary
Allow $or and $and in validation rule expression
Original Description
From MongoDB 3.2, we can validate our documents during update or insertion, but we can't use the $or and the $and operators for complex validation rules.
db.createCollection( "collection", { "validator": { "$and": [ { "a": { "$type": "string" } }, { "b": { "$type": "string" } }, { "c": { "$or": [ { "$type": "string" }, "default" ] } ] }} )
Failed with the following error:
{ "ok" : 0, "errmsg" : "unknown operator: $or", "code" : 2 }