首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >loopback: distinct查询

loopback: distinct查询
EN

Stack Overflow用户
提问于 2015-10-20 14:45:34
回答 4查看 6.9K关注 0票数 5

我正在使用Loopback创建Rest API。需要根据集合中的特定列获取不同的数据。我在下面尝试过,但它不起作用,相反,下面的代码片段也在获取重复数据:

代码语言:javascript
运行
复制
this.app.models.location.find(
                {
                    distinct: ("regionName",
                    {state: st})
                }
                ,
                function(err, location){........}

'RegionName‘是' location’集合的属性,我只需要由'st‘表示的选定状态的数据(state是location集合的另一个属性)。谢谢。

EN

回答 4

Stack Overflow用户

发布于 2016-10-20 00:24:45

我在下面两个例子中得到了答案:

1.)没有查询,只是在"Role“集合的"name”字段上有distinct:

代码语言:javascript
运行
复制
var roleCollection = user.app.models.Role.getDataSource().connector.collection(user.app.models.Role.modelName);
roleCollection.distinct( "name",
  function(err, records) {
    if(err) {
      return cb(err);
    } else {
      return cb(null, records);
    }
  });

2.)通过查询,distinct在" role“集合的" name”字段中删除任何名称为“admin”的角色:

代码语言:javascript
运行
复制
var roleCollection = user.app.models.Role.getDataSource().connector.collection(user.app.models.Role.modelName);
roleCollection.distinct( "name", { name: { $ne: 'admin' } },
  function(err, records) {
    if(err) {
      return cb(err);
    } else {
      return cb(null, records);
    }
  });

Loopback.io v2.29.1

票数 4
EN

Stack Overflow用户

发布于 2016-02-28 16:28:13

我现在已经在我的项目中工作了。

这里有一些示例代码(用来帮助解释你的问题),它们应该能做你需要的事情……

代码语言:javascript
运行
复制
// Distinct regions

Locations.regions = function (cb) {
  console.log('Locations.build');
  var ds = Locations.app.datasources.myDS;
  var sql = "SELECT DISTINCT region FROM Locations ORDER BY region"; // here you write your sql query.

  ds.connector.execute(sql, [], function (err, regions) {

    if (err) {
      cb(err, null);
    } else {
      cb(null, regions);
    }

  });

};

Locations.remoteMethod(
  'regions', {
    http: {
      path: '/regions',
      verb: 'get'
    },
    returns: {
      root: true,
      type: 'object'
    }
  }
);

**注意:这是针对MySQL的,但您应该能够修改其他连接器的查询**

票数 2
EN

Stack Overflow用户

发布于 2015-10-21 19:13:49

Loopback框架还没有提供独特的功能。

使用distinct的Mongo数据库查询如下:

代码语言:javascript
运行
复制
db.runCommand ( { distinct: "<collection>", key: "<field>", query: { <query>} })

请参阅以下链接:

https://docs.mongodb.org/manual/reference/command/distinct/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33229757

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档