0
對象數組殼插入語句我有以下集架構已創建並passed
驗證:蒙戈未能驗證
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
聲明模式,因爲它涉及數組。我試圖在谷歌找到解決方案,但找不到與準備類似於上述模式的插入語句相關的任何內容。
請幫助爲一個mongo文檔的此模式生成insert
語句,以在mongo shell上運行插入查詢。
我知道你在尋找的是一個有點「超出範圍」 MongoDB的架構驗證。這真的只是在嬰兒期。您希望應用的那種條件在「客戶端邏輯」中得到更好的處理。 –
您能否包含您正在嘗試插入的文檔的示例,並且還描述了您的驗證目標?例如:檢查'default'或'applied'數組的前兩個元素(或者檢查這些數組是否存在?)。還值得注意的是,文檔驗證是一個可選的MongoDB功能,比聲明所有文檔必須符合的模式更靈活 - 只有在插入或更新文檔時纔會檢查驗證規則。看起來您可能會將驗證與您在應用程序代碼中使用的模式或模型定義混爲一談。 – Stennie