简体   繁体   中英

Difference Between Passing an Array of Object Literal To Mongoose Schema

I'm just wondering how come this code returns an _id key in when receiving json from the server.

const productSchema = new mongoose.Schema({
  name: {
    type: String,
    required: [true, 'Please enter product name'],
    trim: true,
    maxLength: [100, 'Product name cannot exceed 100 characters'],
  },
  price: {
    type: Number,
    required: [true, 'Please enter product price'],
    maxLength: [5, 'Product price cannot exceed 5 characters'],
    default: 0.0,
  },
  description: {
    type: String,
    required: [true, 'Please enter product description'],
  },
  ratings: {
    type: Number,
    default: 0,
  },
  images: [
    {
      public_id: {
        type: String,
        required: true,
      },
      url: {
        type: String,
        required: true,
      },
    },
  ],
"images": 
[
            {
                "_id": "6070a2351c2fe84ab8883e4c",
                "public_id": "products/dsvbpny402gelwugv2le",
                "url": "https://res.cloudinary.com/bookit/image/upload/v1608062030/products/dsvbpny402gelwugv2le.jpg"
            }
        ],

I noticed that the _id field is there when requesting from the server. Is this an automatic thing?

You should know that by default, Mongoose adds an _id property to your schemas.

You can know more here: https://mongoosejs.com/docs/guide.html#_id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM