Mongodb mongoose模式中的嵌套映射

Mongodb mongoose模式中的嵌套映射,mongodb,mongoose,mongoose-schema,Mongodb,Mongoose,Mongoose Schema,我目前正在为我们的新JSON格式创建一个合适的mongoose模式。 这并不十分复杂,但我遇到了一个问题,即某些值不是保存为数组,而是保存为“规范化数组”,如下所示: answers: [{value: 5, string: "abc"}, {value: 4, string: "def"}] will be: answers: { 1: {id: 1, value: 5, string: "abc"}, 2: {id: 2, value: 4,

我目前正在为我们的新JSON格式创建一个合适的mongoose模式。 这并不十分复杂,但我遇到了一个问题,即某些值不是保存为数组,而是保存为“规范化数组”,如下所示:

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
}
对象本身也可以具有嵌套的“规范化数组”

目前,我尝试在顶级模式中使用mongoose类型“Map”,如下所示:

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.
为什么我不能像预期的那样嵌套贴图?甚至有可能在mongoose模式中投射这种“规范化数组”结构吗?如果有,怎么做


感谢阅读

我当时也处于同样的情况,这似乎对我有用:

const ChildSchema = new Schema({
  firstName: String,
});

const ParentSchema = new Schema({
  name: String,
  mapProperty: { type: Map, of: ChildSchema },
});
我还从ChildSchema上的mongoose触发了验证,所以它似乎可以接受它


希望能有所帮助。

你找到这个问题的答案了吗?我已经完成了相同的用例。