获取MongoDB集合中使用环回的所有字段的列表

时间:2017-11-02 12:01:39

标签: angularjs mongodb mongodb-query loopbackjs angular-loopback

我正在使用环回。我在mongodb中有用户表。

我想要用户表的唯一列名(键)。

我在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", ...]

但我不知道如何在环回中做到这一点。

任何人都可以告诉我如何在环回中实现上述代码。

1 个答案:

答案 0 :(得分:0)

您可以通过以下几种方式使用字段过滤器:

REST API

这适用于一个或多个财产。

true包含字段

false排除某个字段

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

适用于您的情况:

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

NODE API

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

适用于您的情况:

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

HERE the official documentation of the explanation above.

MODEL DEFINITION JSON

您可以在JSON模型文件(user.json)中使用scope属性,但不建议这样做,因为它始终只能找到names字段。

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

Check this official documentation for more information about SCOPES

ANGULARJS过滤器

this is the documentation for angularjs filters.