调试文档验证错误

时间:2017-10-26 02:09:39

标签: node.js mongodb

来自mongo shell:db.getCollectionInfos({ name : 'applications' })返回:

[
    {
        "name" : "applications",
        "type" : "collection",
        "options" : {
            "validator" : {
                "$and" : [
                    {
                        "user_name" : {
                            "$type" : "string"
                        }
                    },
                    {
                        "password" : {
                            "$type" : "string"
                        }
                    },
                    {
                        "role" : {
                            "$type" : "bool"
                        }
                    },
                    {
                        "deposit_amount" : {
                            "$type" : "double"
                        }
                    },
                    {
                        "submit_date" : {
                            "$type" : "int"
                        }
                    },
                    {
                        "user_ip" : {
                            "$type" : "string"
                        }
                    }
                ]
            },
            "validationLevel" : "strict",
            "validationAction" : "error"
        },
        "info" : {
            "readOnly" : false
        },
        "idIndex" : {
            "v" : 2,
            "key" : {
                "_id" : 1
            },
            "name" : "_id_",
            "ns" : "turk_system.applications"
        }
    }
]

在node.js中,尝试使用官方mongodb驱动程序插入文档:

//...
db.collection('applications').insertOne
(
    {
        'user_name' : 'test123',
        'password' : 'test123',
        'role' : true,
        'deposit_amount' : Number.parseFloat('34.5'),
        'submit_date' : mongodb.Long.fromNumber(500),
        'user_ip' : '192.168.100.1'
    }
)
.then((result) =>
{
    //...
})
.catch((err) =>
{
    console.log(err);
});

捕获错误:

{ MongoError: Document failed validation
    at Function.MongoError.create (/loc/to/node_modules/mongodb-core/lib/error.js:31:11)
    at toError (/loc/to/node_modules/mongodb/lib/utils.js:139:22)
    at /loc/to/node_modules/mongodb/lib/collection.js:748:67
    at /loc/to/node_modules/mongodb-core/lib/connection/pool.js:469:18
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickCallback (internal/process/next_tick.js:180:9)
name: 'MongoError',
message: 'Document failed validation',
driver: true,
index: 0,
code: 121,
errmsg: 'Document failed validation' }

Mongodb文档验证错误并不具有描述性,但我的猜测是 mongo和node.js如何表示类型之间存在一些不匹配,但我不确定在哪里。

我如何解决上述问题?

(mongodb版本:v3.4.4,在linux上)

1 个答案:

答案 0 :(得分:1)

不,它不是非常具有描述性,而且基本上都是你得到的。这是一个非常新的功能,所以唯一真正的"调试"你得到的是查看你发送的客户端代码。失败是因为Long返回 64位整数 "定义一个Long类来表示64位二进制补码" ,您的约束正在寻找一个32位整数。它应该是"long"而不是"int"。见$type

        {
            "submit_date" : {
                "$type" : "long"
            }
        },

或者,您使用.toInt()返回 32位整数

{
    'user_name' : 'test123',
    'password' : 'test123',
    'role' : true,
    'deposit_amount' : Number.parseFloat('34.5'),
    'submit_date' : mongodb.Long.fromNumber(500).toInt(),
    'user_ip' : '192.168.100.1'
}

只要它是一个实际可以表示为32位整数的值。

如果您正在寻找"架构"然后你"应该"而是改为寻找客户端实现。 Document Validation并非旨在替代此类功能,您将从"客户端"获得更详细的错误消息。您目前将从服务器提交的提交内容。