How do I return only selected fields from the model and its relation

Mongoose ORM:

strapi.query('products').model.find({}).select({
    title: 1, //include title field
    body: 1, //include body field
    orders: 1 //include title field
}).populate('orders', 'amount'); //populate 'orders' relation and populate its 'amount' field

Will include only id,title, body fields and Orders relation from products model. For Orders relation, we populate only the amount field.

Result:
image



Bookshelf ORM:

strapi.query('products').model.fetchAll({
      columns:['id','title','body'], //populate id, title, body fields for products
      //Please note that its mandatory to get `id` in 'Products' columns, otherwise you won't be able to get Orders relation. 
      withRelated: [
        {
          'orders': qb => { //populate 'Orders' relation
            qb.columns([
              'orders.amount' //populate only the 'amount' field of the 'Orders'
            ]);
          },
        },
      ],
    });

Will include only id,title, body fields and Orders relation from Products model. For Orders relation, we populate only the amount field.

Result:
image

2 Likes