mongodb-js / mongoose-autopopulate

Always populate() certain fields in your mongoose schemas
Apache License 2.0
222 stars 36 forks source link

How to AutoPopulate inside a object #58

Closed suryabigapp closed 4 years ago

suryabigapp commented 4 years ago
status: {
      type: {
        id: {
          type: ObjectId,
          ref: 'CustomerStatus',
          autopopulate: { select: 'title' },
        },
        updatedBy: {
          type: ObjectId,
          ref: 'User',
          autopopulate: { select: 'name' },
        },
        updatedAt: {
          type: Date,
        },
      },
    },

Something like this

vkarpov15 commented 4 years ago

Remove the type key, because as written, your schema defines status as a Mixed path, and autopopulate doesn't look underneath mixed paths.

status: {
      //type: {
        id: {
          type: ObjectId,
          ref: 'CustomerStatus',
          autopopulate: { select: 'title' },
        },
        updatedBy: {
          type: ObjectId,
          ref: 'User',
          autopopulate: { select: 'name' },
        },
        updatedAt: {
          type: Date,
        },
      },
    },
vkarpov15 commented 4 years ago

See Automattic/mongoose#7181