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

Allow default values to be set for new documents

    XMLWordPrintable

    Details

    • Type: New Feature
    • Status: Open
    • Priority: Major - P3
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: Backlog
    • Component/s: Querying
    • Labels:

      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

        Attachments

          Activity

            People

            • Votes:
              2 Vote for this issue
              Watchers:
              19 Start watching this issue

              Dates

              • Created:
                Updated: