-1

I am currently using StrongLoop as my API backend server and Mongodb as data storage engine.

Let's say there is a collection called article. It has two fields title, and content. And there are two frontend pages to display a list of articles and view a single article.

Obviously the data list page only need title field and the view page need both. Currently the GET method of StrongLoop API return all fields including content. It cost extra traffic. Is there any way that can just return specific field?

Mongodb support projection in find() method for this. How can I do the same thing by StrongLoop?

Deduplicator
  • 41,806
  • 6
  • 61
  • 104

3 Answers3

2

Have you taken a look at the filters offered. http://docs.strongloop.com/display/LB/Querying+models

snathan
  • 525
  • 2
  • 7
2

Query for NodeAPI:

server.models.Student.findOne({where: {RFID: id},fields: {id: true,schoolId: true,classId: true}}, function (err, data) {
                        if (err) 
                         callback(err);
                         else {
                            callback();
                        }
                    })

Query for RestAPI :

 $http.get('http://localhost:3000/api/services?filter[fields][id]=true&filter[fields][make]=true&filter[fields][model]=true')
                .then(function (response) {

                }, function (error) {

                });
bhaRATh
  • 159
  • 12
0

You can use fields projections,

Sample Record:

{ name: 'Something', title: 'mr', description: 'some desc', patient: { name: 'Asvf', age: 20, address: { street: 1 }}}

First Level Projection:

model.find({ fields: { name: 1, description: 1, title:  0 } })

and I think Strong loop is not yet supporting for second-level object filter, does anyone know how to filter second-level object properties or is yet to implement?.

Second Level Projection: (Need help here)

Ex: 2

model.find({ fields: { name: 1, 'patient.name': 1, 'patient.age': 1, 'patient.address': 0 } })
// Which results { name } only
Naren
  • 2,976
  • 3
  • 9
  • 20
  • Are you asking a question or providing a solution? – Dharman Apr 27 '20 at 13:56
  • Actually both, the first example for the first level projection solution and Asking for the second-level projection (in Strongloop). read the second example. – Naren Apr 28 '20 at 05:42