猫鼬为未知键定义架构

时间:2020-02-03 08:03:40

标签: reactjs mongodb mongoose mongoose-schema

我的MongoDB存储具有以下结构的文档:

{
    "application_detail":{},
    "curl_detail":{
        "Curl1":{
            "key1":"value1",
            "key2":"value2"
        },
        "Curl2":{
            "key1":"value1",
            "key2":"value2"        
        },
        "Curl3":{
            "key1":"value1",
            "key2":"value2"
        },
        "Curl4":{
            "key1":"value1",
            "key2":"value2"
        },
        /*total number of curls are unknown*/
    }
}

现在,我正在使用猫鼬为key1中存在的每个Curl仅获取curl_detail的值。

如何为此类文档定义架构?

1 个答案:

答案 0 :(得分:1)

您可以使用Map schema type

const schema = new mongoose.Schema({
  curl_detail: {
    type: mongoose.Schema.Types.Map,
    of: {
      key1: String,
      key2: String
    }
  }
})