【问题标题】:Mongodb v4 Document Validation FailedMongodb v4 文档验证失败
【发布时间】:2019-05-19 16:35:41
【问题描述】:

我对 mongo 比较陌生,在我们的生产环境中遇到了一个验证问题,而在我们的开发环境中似乎没有发生。

我认为这是由于在 dev 中验证级别未设置为严格。

我现在已经为此浪费了几个小时,所以我想联系一下,看看我们是否有任何 mongo 专家可以帮助我。

下面是我们的集合的 jsonSchema 和验证失败的文档。

有谁知道验证失败的原因。愚蠢的 mongo 没有给出任何有用的错误信息。

{
"validator" : {
    "$jsonSchema" : {
        "bsonType" : "object",
        "required" : [
            "name",
            "subdomain",
            "userLimit",
            "isTrial",
            "trialExpiryDate"
        ],
        "properties" : {
            "name" : {
                "bsonType" : "string"
            },
            "subdomain" : {
                "bsonType" : "string"
            },
            "userLimit" : {
                "bsonType" : "int"
            },
            "isTrial" : {
                "bsonType" : "bool"
            },
            "trialExpiryDate" : {
                "bsonType" : "date"
            },
            "viewDisplayOrders": {
                "bsonType" : "array"
            }
        }
    }
},
"validationLevel" : "strict",
"validationAction" : "error"}

{
"_id" : ObjectId("5cc71aae77a8801149aab04d"),
"name" : "Mushroom Kingdom",
"subdomain" : "mushroom-kingdom",
"viewDisplayOrders" : [
    {
        "_id" : "5cc72902e2d2f112be40c937",
        "displayOrder" : 0
    },
    {
        "_id" : "5cc72dd5e2d2f112be40c96c",
        "displayOrder" : 1
    },
    {
        "_id" : "5cc74fbfe2d2f112be40ccba",
        "displayOrder" : 2
    },
    {
        "_id" : "5cc75658e2d2f112be40cf78",
        "displayOrder" : 3
    },
    {
        "_id" : "5cc720b148ba7011394abcbf",
        "displayOrder" : 4
    },
    {
        "_id" : "5cc8c32a5c62ac1ea3dd3d5b",
        "displayOrder" : 5
    }
],
"isTrial" : true,
"userLimit" : 10,
"trialExpiryDate" : ISODate("2019-05-28T00:00:00Z")

}

【问题讨论】:

  • 即使没有用,错误信息是什么?
  • @Eric MongoError: 在 toError (/home) 处的 Function.create (/home/ec2-user/server/node_modules/mongodb-core/lib/error.js:43:12) 文档验证失败/ec2-user/server/node_modules/mongodb/lib/utils.js:149:22) 在 coll.s.topology.update (/home/ec2-user/server/node_modules/mongodb/lib/operations/collection_ops.js :1465:39) 在 /home/ec2-user/server/node_modules/mongodb-core/lib/connection/pool.js:397:18 在 process._tickCallback (internal/process/next_tick.js:61:11)
  • @Eric 不确定是否值得注意我使用 db.runCommand({collMod: "col", validator: validator }); 更新了集合验证器;因为它最初是创建的。
  • 如果您向属性添加描述,是否会返回失败属性的描述?您可以尝试一下,例如添加“description”:“testA”,然后对所有字段(testB、testC ...)执行此操作,看看您是否有罪魁祸首属性。
  • 我认为您的错误与日期有关。您可以尝试在架构和文档中删除它吗?

标签: mongodb jsonschema


【解决方案1】:

错误与日期字段有关(ISOdate vs ISO vs "date" bsonType)

【讨论】:

    猜你喜欢
    • 2020-03-16
    • 1970-01-01
    • 2019-04-28
    • 1970-01-01
    • 1970-01-01
    • 2016-11-06
    • 2016-09-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多