猫鼬中{}和[]之间的区别?

时间:2018-11-23 05:57:47

标签: node.js mongodb mongoose nosql

{}和[]之间的主要区别是什么?

我想知道何时使用它以及如何存储数据

education: [
    {
      school: {
        type: String,
        required: true
      },
      fieldofstudy: {
        type: String,
        required: true
      },
      from: {
        type: Date,
        required: true
      },
      to: {
        type: Date
      },
      current: {
        type: Boolean,
        default: false
      },
      description: {
        type: String
      }
    }
  ]


social: {
    youtube: {
      type: String
    },
    twitter: {
      type: String
    },
    facebook: {
      type: String
    },
    linkedin: {
      type: String
    },
    instagram: {
      type: String
    }
  }

我希望看到一些示例,以了解在哪里使用以及何时不使用它。

1 个答案:

答案 0 :(得分:0)

{ }可以包含索引数据(带有字符串索引),而[ ]只能包含未索引的数据(默认情况下为数字索引)。

如您的示例所示,

  1. 此处,默认情况下数据在0索引上:

    education: [{
      school: {
        type: String,
        required: true
      },
      fieldofstudy: {
        type: String,
        required: true
      },
      from: {
        type: Date,
        required: true
      },
      to: {
        type: Date
      },
      current: {
        type: Boolean,
        default: false
      },
      description: {
        type: String
      }
    }]
  2. 这里的数据已为字符串索引

    social: {
        youtube: {
          type: String
        },
        twitter: {
          type: String
        },
        facebook: {
          type: String
        },
        linkedin: {
          type: String
        },
        instagram: {
          type: String
        }
    }