[SERVER-24430] Allow default values to be set for new documents Created: 07/Jun/16  Updated: 07/Feb/23

Status: Backlog
Project: Core Server
Component/s: Querying
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: Styvane Soukossi Assignee: Backlog - Query Execution
Resolution: Unresolved Votes: 9
Labels: validation
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Assigned Teams:
Query Execution
Participants:
Case:

 Description   

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 }

source: https://stackoverflow.com/questions/37674538/how-can-i-ensure-that-only-certain-keys-are-present-in-a-document-using-mongodb



 Comments   
Comment by Katya Kamenieva [ 05/Apr/21 ]

This request is not currently scheduled and remains in the backlog. To all the interested users, please feel free to add comments with details of your use case for this feature.

Comment by Deepak SP [ 02/Apr/21 ]

Is anyone working on this issue? 

Comment by Kelsey Schubert [ 08/Jun/16 ]

Hi styvane,

Thanks for confirming! I've marked this ticket to be considered during the next round of planning. Please continue to watch for updates.

Kind regards,
Thomas

Comment by Styvane Soukossi [ 07/Jun/16 ]

Hi anonymous.user,

Yes, this exactly the behavior I would like to see implemented.

Thank you

Comment by Kelsey Schubert [ 07/Jun/16 ]

Hi styvane,

I'd like to make sure I'm understanding this feature request. Please review my theoretical example below with made up syntax below. While this feature will most likely utilize a different syntax if it is implemented, I would first like to make sure we are the same page.

> 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"
}
> db.coll.insert({ "a": "A", "b": "B", "c": "C" })
WriteResult({ "nInserted" : 1 })
> db.coll.insert({ "a": "A", "b": "B", "c" : 1 })
WriteResult({
	"nInserted" : 0,
	"writeError" : {
		"code" : 121,
		"errmsg" : "Document failed validation"
	}
})

Is this the behavior you would like to see implemented? If not, would you please clarify your request with additional examples?

Thank you,
Thomas

Comment by Styvane Soukossi [ 07/Jun/16 ]

What I want for example is assign a "default" to "c" if the "c" field is not present in the document being inserted.

Comment by Daniel Pasette (Inactive) [ 07/Jun/16 ]

What document are you trying to validate? Include your update or insert operator that is failing.

Also, it is not clear what you are trying to accomplish in your $or clause, because you are checking that "c" is a string or if the value of "c" is equal to the string "default"

Comment by Styvane Soukossi [ 07/Jun/16 ]

I got the `$or` syntax wrong here, as it should be:

{ "$or": [{ 'c': { "$type": "string"}} , {'c': "default"} ]}

Anyway that does solve the problem. We still have validation error if we insert something like this:

 db.collection.insert( { "a": "any_value",  "b": "any_other_value"  } )

It yields:

WriteResult({
        "nInserted" : 0,
        "writeError" : {
                "code" : 121,
                "errmsg" : "Document failed validation"
        }
})

This is because in our validation rule

{'c': "default"}

means "c" equals "default".

Generated at Thu Feb 08 04:06:20 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.