首页 > 解决方案 > 如何在 golang 应用程序中添加 MongoDb 模式验证器

问题描述

我在我的 go 项目中为 mongo db 使用以下验证器模式,但由于当前不支持字段“验证器”而出现错误。

{

var jsonSchema = bson.M{
        "bsonType":             "object",
        "required":             []string{"itemId", "siteId"},
        "additionalProperties": false,
        "properties": bson.M{
            "itemId": bson.M{
                "bsonType":    "long",
                "description": "the itemId is long int",
            },
            "siteId": bson.M{
                "bsonType":    "int",
                "description": "the siteId is int",
            },
        },
    }
    var validator = bson.M{
        "$jsonSchema": jsonSchema,
    }
    opts := options.CreateCollection().SetValidator(validator)
    err = ds.database.CreateCollection(context.Background(), "InventoryDetails", opts)

}

下面是我的 mongo db 驱动版本 go.mongodb.org/mongo-driver v1.7.3

标签: mongodbgo

解决方案


推荐阅读