Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-24430

Allow default values to be set for new documents

    • Type: Icon: New Feature New Feature
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Querying
    • Query Execution

      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

            Assignee:
            backlog-query-execution [DO NOT USE] Backlog - Query Execution
            Reporter:
            styvane Styvane Soukossi
            Votes:
            9 Vote for this issue
            Watchers:
            25 Start watching this issue

              Created:
              Updated: