mongodb - 如何在 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
解决方案
推荐阅读
- java - 如何使用cardview和适配器将按钮添加到recyclerview布局中?
- javascript - 将数据从页面发送到 chrome 扩展
- c# - AWS Lambda - C# - 环境变量只加载空
- python - 在张量流中加载现有模型时出现问题
- python - ZMQ 上下文套接字在新计算机上的创建速度要慢得多
- javascript - Safari 手机上看不到汉堡菜单
- karate - 空手道 API:从其他文件夹调用功能文件时找不到文件异常
- time - 在线性时间内查找未排序列表的前 log n 个元素
- cypress - 让赛普拉斯与 StencilJS 组件配合使用时出现问题
- swift - 如果语句在 Swift 4 中的行为不符合预期