首页 > 解决方案 > MongoDB 模式验证在更新时失败,但没有一次插入

问题描述

我有一个带有模式验证的 Mongodb 集合。我执行db.correspondence.validate({full:true})并收到"nInvalidDocuments" : NumberLong(0)

可以插入文档,但更新失败

MongoDB Enterprise > db.correspondence.find({"correspondenceIdentifier": "ca4697e2-a40c-11ea-a632-0a0a6b0e0000"}).count() 
8

MongoDB Enterprise > db.correspondence.insert({"correspondenceIdentifier": "ca4697e2-a40c-11ea-a632-0a0a6b0e0000",mdmContractIdentifier:'3334444444444444','name':'Vat'});
WriteResult({ "nInserted" : 1 })

MongoDB Enterprise > db.correspondence.find({"correspondenceIdentifier": "ca4697e2-a40c-11ea-a632-0a0a6b0e0000"}).count()
9

MongoDB Enterprise > db.correspondence.find({mdmContractIdentifier:'3334444444444444'}).count()
2

MongoDB Enterprise > db.correspondence.updateOne({"correspondenceIdentifier": "ca4697e2-a40c-11ea-a632-0a0a6b0e0000"},{$set:{mdmContractIdentifier:'3334444444444444'}});
2020-08-05T20:50:28.164-0400 E QUERY    [thread1] **WriteError: Document failed validation** :
WriteError({
        "index" : 0,
        "code" : 121,
        "errmsg" : "Document failed validation",
        "op" : {
                "q" : {
                        "correspondenceIdentifier" : "ca4697e2-a40c-11ea-a632-0a0a6b0e0000"
                },
                "u" : {
                        "$set" : {
                                "mdmContractIdentifier" : "3334444444444444"
                        }
                },
                "multi" : false,
                "upsert" : false
        }
})`
WriteError@src/mongo/shell/bulk_api.js:466:48
Bulk/mergeBatchResults@src/mongo/shell/bulk_api.js:846:49
Bulk/executeBatch@src/mongo/shell/bulk_api.js:910:13
Bulk/this.execute@src/mongo/shell/bulk_api.js:1154:21
DBCollection.prototype.updateOne@src/mongo/shell/crud_api.js:572:17
@(shell):1:1

标签: mongodbvalidationschema

解决方案


推荐阅读