【问题标题】:sails waterline sort on number not working(sails-redis)帆水线排序编号不起作用(sails-redis)
【发布时间】:2018-01-03 23:35:00
【问题描述】:

我正在尝试排序 id

模态:

module.exports = { autoPK: false, attributes: { id: { type: 'integer', autoIncrement:true, primaryKey: true }, } }

查询:

mymodal.find().sort({id: 'asc'}).exec(function (err, res) {
    console.log(res)
});

数据:

[ { id: '2', },{ id: '1'},{ id: '11' } ]

实际:

[ { id: '1', },{ id: '11'},{ id: '2' } ]

**预期:

[ { id: '1', },{ id: '2'},{ id: '11' } ]**

谁能帮帮我。请..

按字符串排序,但按数字(整数)排序​​。

帆水线标准排序

是否有我的查询或问题

【问题讨论】:

  • 当你在你的数据库中观察时,你确定如果“id”是整数类型,你在local.js中的migrate等于“alter”吗?你应该复制你的 JSON 结果,只有 find()。
  • 是的.. 我确定在我的模型 ID -> 类型:'integer',autoIncrement:true,primaryKey:true。您是否尝试过按 id 排序,其中 id 是数字。使用sails-redis。请帮帮我?

标签: sorting sails.js waterline sails-redis


【解决方案1】:

首先你应该做查询 find 然后排序()

Modal.find()
.sort({id: 'asc'})
.exec(function(err, res) {
  console.log(res)
});

【讨论】:

  • 抱歉这个错误,我已经更正了我在帖子中的查询。任何帮助
  • 需要的是帆水线按数字而不是按字符串排序。有没有人尝试过或遇到过这个问题,还有其他选择吗?
【解决方案2】:

我浏览了sails-redis 适配器索引和架构,缺少一些东西。在解析像'11'这样的输入数据时,该方法不担心数据类型。 即使在模型中,像 { id: 1 } 和 { id: '1'} 这样的数据也被认为是不同的 type: 'integer 指定。

sails-redis/lib/database/schema.js

更改代码: Schema.prototype.parse . . case 'integer': values[key] = parseInt(values[key]); break; case 'float': values[key] = parseFloat(values[key]); break; } } . . 改变对我有用。确保 values[key] 不为空

【讨论】:

    【解决方案3】:

    The documentation 在这方面不是很好,但如果您将对象传递给sort(),您可能需要使用二进制表示法。更容易传递一个字符串:

    Model.find().sort('id asc').exec(function (err, res) {
      console.log(res)
    });
    

    【讨论】:

      猜你喜欢
      • 2014-01-11
      • 2014-08-23
      • 2016-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多