如何使用 mongodb 验证器验证对象数组? [英] How do I validated array of objects using mongodb validator?

查看:31
本文介绍了如何使用 mongodb 验证器验证对象数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用 MongoDB 提供的验证器来验证我的数据,但我遇到了问题.这是我要插入的一个简单的用户文档.

I have been trying to validate my data using the validators provided by MongoDB but I have run into a problem. Here is a simple user document which I am inserting.

{
    "name"    : "foo",
    "surname" : "bar",
    "books"   : [
      {
        "name" : "ABC",
        "no"   : 19
      },
      {
        "name" : "DEF",
        "no"   : 64
      },
      {
        "name" : "GHI",
        "no" : 245
      }
    ]
}

现在,这是已应用于用户集合的验证器.但这现在适用于我与文档一起插入的书籍数组.我想检查对象内的元素,这些元素是书籍数组的成员.对象的架构不会改变.

Now, this is the validator which has been applied for the user collection. But this is now working for the books array which I am inserting along with the document. I want to check the elements inside the object which are the members of books array. The schema of the object won't change.

db.runCommand({
  collMod: "users",
  validator: {
    $or : [
      { "name"       : { $type : "string" }},
      { "surname"    : { $type : "string" }},
      { "books.name" : { $type : "string" }},
      { "books.no"   : { $type : "number" }}
    ],
  validationLevel: "strict"
});

我知道这个验证器是用于成员对象而不是数组,但是我如何验证这样的对象?

I know that this validator is for member objects and not for array, but then how do I validate such an object ?

推荐答案

这个问题已经很久了.
无论如何,如果有人通过这个.

It has been very long since this question was asked.
Anyways, if at all anyone comes through this.

对于 MongoDB 3.6 及更高版本,这可以使用验证器来实现.

For MongoDB 3.6 and greater version, this can be achieved using the validator.


 db.createCollection("users", {
    validator: {
       $jsonSchema: {
          bsonType: "object",
          required: ["name","surname","books"],
          properties: {
            name: {
                bsonType: "string",
                description: "must be a string and is required"
             },    
             surname: {
                bsonType: "string",
                description: "must be a string and is required"
             },         
             books: {
                bsonType: [ "array" ],
                items: {
                    bsonType: "object",
                    required:["name","no"],
                    properties:{
                        name:{
                            bsonType: "string",
                            description: "must be a string and is required"
                        },
                        no:{
                            bsonType: "number",
                            description: "must be a number and is required"
                        }
                    }
                },
                description: "must be a array of objects containing name and no"
             }
          }
       }
    }
 })

这个可以满足您的所有要求.
有关更多信息,请参阅此链接

This one handles all your requirements.
For more information, refer this link

这篇关于如何使用 mongodb 验证器验证对象数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆