【问题标题】:SailsJS Perform sorting on populate dataSailsJS 对填充数据执行排序
【发布时间】:2017-04-03 06:27:49
【问题描述】:

这是我目前的事件模型

module.exports = {
    attributes: {
        name: {
            type: 'string',
            required: true,
            unique: true
        },
        client: {
            model: 'client',
            required: true
        },
        location: {
            model: 'location',
            required: true
        }
    }
};

客户端模型:

module.exports = {
    attributes: {
        name: {
            type: 'string',
            required: true,
            unique: true
        },
        address: {
            type: 'string'
        },
        clientContact: {
            model: 'user',
            required: true
        }
    }
};

那么我如何实现基于 client name 的排序并同时具有 skiplimit 属性(对于分页)一起工作。

我尝试使用以下查询:

Event.find({ id: ['583eb530902db3d926345215', '583eb6dd91b972ee26dd97b1'] },
           { select: ['name', 'client'] })
           .populate('client', { sort: 'name DESC' })
           .exec((err, resp) => resp.map(r => console.log(r.name, r.client)));

但这似乎没有这样做。

【问题讨论】:

标签: node.js mongodb sails.js waterline sails-mongo


【解决方案1】:

Waterline 不支持像这样按子记录对结果进行排序。如果client 是附加到事件的子记录的集合,那么您的代码将按名称对每个返回的Event 记录填充的client 数组进行排序,但我假设在这个case client 是一个单数关联(即model: 'client')。

如果您想要的是一个按客户名称排序的 Event 记录数组,您可以在检索记录后使用 Lodash 相对轻松地做到这一点:

var _ = require('lodash');

Event.find(queryCriteria).populate('client').exec((err, resp) => {
  if (err) { /* handle error */ }
  var sortedRecords = _.sortBy(eventRecords, (record) => { return record.client.name; });
});

【讨论】:

  • 但是 Lodash 不允许我对数据进行分页。还是谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-11
相关资源
最近更新 更多