Mongodb 验证失败的对象数组的Mongo shell insert语句
我有以下集合模式,该模式已创建且Mongodb 验证失败的对象数组的Mongo shell insert语句,mongodb,mongodb-query,mongo-shell,Mongodb,Mongodb Query,Mongo Shell,我有以下集合模式,该模式已创建且已通过验证: db.createCollection("Settings", { validator: { $or: [ {cID:{ $type: "int" } }, {sID : { $type: "int" } }, {default : [ {default1: {
已通过验证:
db.createCollection("Settings",
{ validator: { $or:
[
{cID:{ $type: "int" } },
{sID : { $type: "int" } },
{default :
[
{default1:
{
name : { $type: "string" },
priority : { $type: "string" },
autoMoveToCompleted : { $type: "string" },
notifyInAdvance : { $type: "string" }
}
},
{default2 :
{
name : { $type: "string" },
priority : { $type: "string" },
autoMoveToCompleted : { $type: "string" }
}
}
]
},
{applied: [
{applied1:
{ name : { $type: "string" },
priority : { $type: "string" },
autoMoveToCompleted : { $type: "string" },
notifyInAdvance : { $type: "string" }
}
},
{applied2 :
{
name : { $type: "string" },
priority : { $type: "string" },
autoMoveToCompleted : { $type: "string" }
}
}
]}
]
}
}
)
但是,我无法为上述模式生成有效的insert
语句,因为它涉及数组。我试图在google中找到解决方案,但找不到任何与准备类似于上述模式的insert语句相关的内容
请帮助为一个mongo文档生成此架构的insert
语句,以便在mongo shell上运行insert查询。我只能建议更改验证架构,如:
db.createCollection("Settings",
{ validator: { $or:
[
{cID:{ $type: "int" } },
{sID : { $type: "int" } },
{"default.default1.name" : { $type: "string" },
"default.default1.priority" : { $type: "string" },
"default.default1.autoMoveToCompleted" : { $type: "string" },
"default.default1.notifyInAdvance" : { $type: "string" },
"default.default2.name" : { $type: "string" },
"default.default2.priority" : { $type: "string" },
"default.default2.autoMoveToCompleted" : { $type: "string" }
}
]
}
}
)
它必须工作我认为您需要的是MongoDB模式验证有点“超出范围”。这真的只是在婴儿期。在“客户端逻辑”中可以更好地处理您希望应用的条件。您能否提供一个您试图插入的文档示例,并描述您的验证目标?例如:检查默认值
或应用的
数组的前两个元素(或者检查这些是否为数组,如果存在的话?)。还值得注意的是,文档验证是一个可选的MongoDB特性,比声明所有文档都必须遵守的模式更灵活——只有在插入或更新文档时才会检查验证规则。看起来您可能将验证与应用程序代码中使用的模式或模型定义混为一谈。