arrays - 正确的语法使用验证规则将数组插入 MongoDB
问题描述
我使用以下语法创建了一个带有验证规则的集合:
>db.createCollection("CollName", {
validator: {
$jsonSchema:
{
bsonType: "object",
properties: {
a1: {
bsonType: "string",
},
a2:{
bsonType: "string",
},
a3: {
bsonType: "array",
items: {
bsonType: "object",
properties: {
b1: {
bsonType: "string",
},
b2: {
bsonType: "string",
}
}
}
}
}
}
}})
我想在其中插入一些文档,但是我找不到正确的语法,我尝试了以下方法:
> db.CollName.insert({
... a1:"122234343",
... a2: "name1",
... a3: [b1: "aaa1111", b2: "bbb222"]
... })
2020-05-28T12:33:50.052+0200 E QUERY [js] uncaught exception: SyntaxError: missing ] after element list :
@(shell):4:8
> db.CollName.insert({
... a1:"122234343",
... a2: "name1",
... a3: ["aaa1111", "bbb222"]
... })
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 121,
"errmsg" : "Document failed validation"
}
})
你能帮我找到正确的语法吗?
解决方案
尝试这个
db.CollName.insert({
"a1":"122234343",
"a2": "name1",
"a3": [{"b1": "aaa1111", "b2": "bbb222"}]
})
推荐阅读
- python - 验证用户输入是否为多个零值
- flutter - Flutter 图像选择器在带有 Mac M1 芯片模拟器的 ios 14 中不起作用
- regex - 正则表达式中多次出现
- linux - RPi 4 写入了 23GB 的日志文件“urb status -32”
- mysql - 如何在一列中对不同的行进行分组?
- javascript - React:如何使用带有复选框的反应表,以便如果选中它,则选择完整的行并将其推送/分配给输入字段
- python - 日期时间绘图随着日期的变化而改变格式
- python - 套接字导入发送变量帮助(Python)
- php - 如何在 Laravel 的 map 方法中使用 array_push?
- javascript - SyntaxError:节点 js 中出现意外的令牌“导出”错误