Mongo, populate nested field only from collection

System Information
  • Strapi Version: 3.4.0
  • Operating System: MacOS
  • Database: MongoDB
  • Node Version: v14.15.3

Hi there,
how i can populate only nested field from collection?

Model

"attributes": {
  "admin": {
    "plugin": "users-permissions",
    "model": "user",
    "via": "restaurant"
  },
  "users": {
    "plugin": "users-permissions",
    "collection": "user",
    "via": "restaurant"
  }
}

Original query

populate = [];
// model, working as well, returned only user id in admin field
populate = ['admin.id'];
// collection, working as well, returned all populated users with nested fields
populate = ['users'];
// collection, but how i can get only users.id from collection?
populate = ['users.id'];

strapi.query('restaurant').findOne({ id: params.id }, populate);

Expected result

{
  admin: ID,
  users: [ID, ID, ID], // not user model
}

You can use autoPopulate for this. Set it to false for the users model. Then in populate define only users.id

populate = [‘users.id’];

https://strapi.io/documentation/developer-docs/latest/concepts/models.html#validations

1 Like