MongoDB jsonSchema validation additionalProperties

前端 未结 1 643
无人及你
无人及你 2021-01-14 02:06

Create Collection with validator

db.createCollection(\"claims\", 
    { validator : { $jsonSchema : { bsonType : \"object\", 
                       


        
相关标签:
1条回答
  • 2021-01-14 02:07

    As at MongoDB 3.6.2, JSON Schema validation does not automatically add the default _id property, so you need to include a rule for this when using additionalProperties: false.

    For example, assuming the default ObjectID:

    db.createCollection("claims",
        { validator : {
            $jsonSchema : {
                bsonType : "object",
                properties : {
                    _id: { bsonType: "objectId" },
                    airportCode : { bsonType: "string"}
                },
                additionalProperties: false
            }
         }}
    )
    

    Two related issues to upvote/watch on the MongoDB Jira issue tracker:

    • SERVER-32160: provide warning when _id is not in list of properties and additionalProperties is false
    • SERVER-20547: Expose the reason an operation fails document validation
    0 讨论(0)
提交回复
热议问题