Arrays 正确语法使用验证规则将数组插入MongoDB
我使用以下语法创建了具有验证规则的集合:Arrays 正确语法使用验证规则将数组插入MongoDB,arrays,mongodb,validation,Arrays,Mongodb,Validation,我使用以下语法创建了具有验证规则的集合: >db.createCollection("CollName", { validator: { $jsonSchema: { bsonType: "object", properties: { a1: { bsonType: "string", }, a2:{ bsonType: "string", },
>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"}]
})