0

What is the main difference between { } and [ ] ?

I want to know when to use it and how stores the data

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
    }
  }

I would love to see some examples to understand where to use it and when not to use it.

Manfred Tijerino
  • 377
  • 1
  • 3
  • 12
  • 2
    https://www.google.com/search?q=javascript+arrays+and+objects – Neil Lunn Nov 23 '18 at 06:02
  • 2
    Though the question likely illustrates a lack of understanding for JavaScript itself and the other references will help, then I would also suggest reading the MongoDB manual section on [Data Modeling](https://docs.mongodb.com/manual/data-modeling/) as a good place to start – Neil Lunn Nov 23 '18 at 06:13

1 Answers1

0

{ } can contain index data (with string indexes), while [ ] can only contain unindexed data (which numerically indexed by default).

As your example suggests,

  1. Here, data is on 0 index by default:

    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. Here data have string indexed

    social: {
        youtube: {
          type: String
        },
        twitter: {
          type: String
        },
        facebook: {
          type: String
        },
        linkedin: {
          type: String
        },
        instagram: {
          type: String
        }
    }
enxtur
  • 2,395
  • 1
  • 16
  • 16
Milind Singh
  • 296
  • 6
  • 23
  • 1
    `while [ ] can only contain unindexed data` This is unfortunately false. Arrays can contain pretty much the same sort of key-value pairs as objects, it's just an abuse of their structure to do so. Also, your code doesn't look great, strings require delimiters. – CertainPerformance Nov 23 '18 at 06:07
  • @CertainPerformance I know its "(which numerically indexed by default)" as added. Also, your code doesn't look great, strings require delimiters. I didn't get?? – Milind Singh Nov 23 '18 at 06:09