【发布时间】:2019-01-16 15:01:59
【问题描述】:
我目前正在为我们的新 JSON 格式创建一个合适的猫鼬模式。 它不是很复杂,但我遇到了某些值没有保存为数组而是像这样的“标准化数组”的问题:
answers: [{value: 5, string: "abc"}, {value: 4, string: "def"}]
will be:
answers: {
1: {id: 1, value: 5, string: "abc"},
2: {id: 2, value: 4, string: "def"}
}
对象本身也可以有嵌套的“标准化数组”。
现在我尝试在顶级架构中使用猫鼬类型“地图”,如下所示:
answers: {
type: Map,
of: answer
}
其中“answer”是一个单独的 mongoose.Schema 本身。 但我得到的只是:
TypeError: Undefined type `Map` at `answers`
Did you try nesting Schemas? You can only nest using refs or arrays.
为什么我不能按预期嵌套地图?甚至可以在猫鼬模式中投影这种“标准化数组”结构,如果可以,如何?
感谢阅读!
【问题讨论】:
-
你找到答案了吗?我已经完成了相同的用例。
标签: mongodb mongoose mongoose-schema