Ankur
Ankur

Reputation: 3199

Get a list of all fields in a MongoDB Collection Using loopback

I am using loopback. I have user table in mongodb.

I want unique column names(keys) of user table.

I have found below answer in mongodb.

mr = db.runCommand({
  "mapreduce" : "my_collection",
  "map" : function() {
    for (var key in this) { emit(key, null); }
  },
  "reduce" : function(key, stuff) { return null; }, 
  "out": "my_collection" + "_keys"
})

db[mr.result].distinct("_id")
["foo", "bar", "baz", "_id", ...]

But i am not sure how to do this in loopback.

Can anyone tell me how to implement above code in loopback.

Upvotes: 0

Views: 570

Answers (1)

Anouar Kacem
Anouar Kacem

Reputation: 645

You can use the field filter in several ways, on your :

REST API

This is for one or more than one property.

true to include a field

false to exclude a field

?filter[fields][propertyName]=<true|false>&filter[fields][propertyName]=<true|false>...

for your case :

?filter[fields][names]=true&filter[fields][id]=false&filter[fields][something]=false

NODE API

model.find({ 
       fields: {
         propertyName: <true|false>, 
         propertyName: <true|false>,
          ... } 
    });

for your case :

 user.find({ 
           fields: {
             names: true, 
             something: false,
              ... } 
        });

HERE the official documentation of the explanation above.

MODEL DEFINITION JSON

You can use the scope property in your JSON model file (user.json) but this is not recommended because it will always find the names field only.

"scope": {
    { fields: {names: true, id: false, field: false} }
  },

Check this official documentation for more information about SCOPES

ANGULARJS Filter

this is the documentation for angularjs filters.

Upvotes: 0

Related Questions