0

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.

Ankur Akvaliya
  • 2,989
  • 4
  • 28
  • 53
  • Why would you need it in loopback? Why would you need this for any other purpose than analysis of your presently stored data? Not the sort of thing that is meant to be part of a production system. – Neil Lunn Nov 02 '17 at 12:07
  • could it be your question is how to filter your collection ? – Anouar Kacem Nov 02 '17 at 15:27
  • @NeilLunn. I need it in loopback because i am using loopback. Purpose of this is my client need dynamic filters on user table. So he need unique column name. From that he will decide Which column he want to display for his login dynamically. – Ankur Akvaliya Nov 03 '17 at 08:48

1 Answers1

0

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.

Anouar Kacem
  • 635
  • 10
  • 24
  • Please read question carefully. It's not about filter on fields. It's about how to get unique field list from table. – Ankur Akvaliya Nov 03 '17 at 11:49
  • @AnkurAkvaliya sorry for missunderstanding, you mean you want a unique name list right ? – Anouar Kacem Nov 04 '17 at 08:52
  • I need unique field(column) names like if collection has two records like `{fname:'..', lname: '...'}` & `{fname:'..', lname: '...', company: '...'}` then i need result like `['fname', 'lname', 'company']` – Ankur Akvaliya Nov 08 '17 at 11:27