添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams
db.createCollection("claims", 
    { validator : { $jsonSchema : { bsonType : "object", 
                 properties : { airportCode : { bsonType: "string"} }, 
                 additionalProperties: false} 
                  } } )

Insert db.claims.insert({"airportCode" : "DSM"}) => result: "errmsg" : "Document failed validation"

if I remove "additionalProperties: false" by creating the collection, when I can insert the document.

Any advice, how to keep "additionalProperties: false", because I want to control the document.

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
  • Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.