【问题标题】:Queries error sorting documents with find Pouchdb使用 find Pouchdb 查询错误排序文档
【发布时间】:2019-08-21 21:22:10
【问题描述】:

我希望使用 pouchdb 对结果进行排序/排序。 我在希望排序大于 882 的字段上创建了一个索引

我已检查数据库是否存在

那么结果是这样的:

883

895

9

909

917

93

我指的是文档:https://pouchdb.com/guides/mango-queries.html 和这个文档:http://docs.couchdb.org/en/stable/api/database/find.html

    this.db= new PouchDB('parcelles', {adapter: 'idb'});

        // Create an index to the field id_culture

            this.db.createIndex({
             index: {
             fields: ['id_culture']
              }
              }).then((result)=> {
                      console.log(result);

              }).catch(function (err) {

              });

        // Query with the sort filter

              this.db.find({
              selector: {
              id_culture: {$gte: '882'}
               },
              sort: ['id_culture']
               }).then( (result)=> {console.log(result);

               }).catch(function (err) {console.log(err);

               });

【问题讨论】:

  • 这些字段排序正确,它们只是按文本表示排序,而不是按数值排序。要按数值排序,最简单的选择是用前导零填充数字。

标签: ionic-framework pouchdb


【解决方案1】:

您的属性id_culture 是文本字符串而不是数字。您将必须决定最大可能大小,例如 100,000,000 并保留您的所有 id。

我也推荐前缀,因此您可以尝试,例如:Culture_00000009、Culture_00000909、Culture_00000093 等。使用这样的 id 排序将起作用....

id_culture: {$gte: 'Culture_00000882'}

...给予...

Culture_00000883
Culture_00000895
Culture_00000909
Culture_00000917
Culture_00001093

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-10
    • 1970-01-01
    • 2016-12-31
    • 1970-01-01
    • 1970-01-01
    • 2015-09-02
    • 1970-01-01
    • 2016-10-06
    相关资源
    最近更新 更多