【问题标题】:No index exists for this sort - couchDB这种排序不存在索引 - couchDB
【发布时间】:2021-09-09 17:45:59
【问题描述】:

使用排序方法访问数据时出错

错误是

此排序不存在索引

创建索引代码是

db.createIndex({
    index: { fields: ["timestamp", "upVote"] },
}).then(() => {
    this.intitialDatafromDB(db);
});

查找函数是

db.find({
   selector: { upVote: { $gt: 5 } },
   sort: [{ upVote: "desc" }],
   fields: ["news"],
}).then((result: any) => {
    this.data = result.docs;
}).catch((err: any) => {
    console.log(err);
});

【问题讨论】:

    标签: couchdb pouchdb


    【解决方案1】:

    您的查询失败的原因是索引字段的顺序很重要

    来自 pouchdb 文档Indexing on more than one field

    需要注意的一点是,这些字段的顺序很重要 创建索引

    通过这样指定索引

    fields: ['timestamp','upVote']
    

    索引是这样的

    timestamp upVote
    1590399369500 3
    1590399369600 4
    1590399369700 1
    1590399369700 2
    1590399369700 3
    1590399369800 1

    注意时间戳1590399369700,以及辅助字段upVote 的排序方式。

    如果您的索引字段是这样排序的

    fields: ['upVote','timestamp']
    

    根据上面的理论数据,索引应该是这样的

    upVote timestamp
    1 1590399369700
    1 1590399369800
    2 1590399369700
    3 1590399369500
    3 1590399369700
    4 1590399369600

    并且您的查询将返回您期望的结果,如下面的 sn-p 所示。我建议阅读Map/reduce queries;掌握该文档中提出的概念可以更深入地理解为什么会这样。

    const g_result = 'result';
    const getEl = id => document.getElementById(id);
    let db;
    
    async function view() {
      const view = getEl(g_result);
    
      const result = await db.find({
        selector: {
          upVote: {
            $gt: 5
          },
        },
        sort: [{
          'upVote': 'desc'
        }],
        fields: ['news','upVote']
      }, );
    
      view.innerText = JSON.stringify(result, undefined, 3);
    }
    
    // canned test documents
    function getDocsToInstall() {
      return [{
          timestamp: 1590399369508,
          upVote: 3,
          news: "new item 1"
        },
        {
          timestamp: 1590399248600,
          upVote: 4,
          news: "new item 2"
        },
        {
          timestamp: 1590399248600,
          upVote: 5,
          news: "new item 3"
        },
        {
          timestamp: 1590399248700,
          upVote: 6,
          news: "new item 4"
        },
        {
          timestamp: 1590399248900,
          upVote: 7,
          news: "new item 5"
        },
        {
          timestamp: 1590399249000,
          upVote: 8,
          news: "new item 6"
        },
      ]
    }
    
    // init example db instance
    async function initDb() {
      db = new PouchDB('test', {
        adapter: 'memory'
      });
    
      await db.bulkDocs(getDocsToInstall());
      await db.createIndex({
        index: {
          fields: ['upVote', 'timestamp']
        }
      });
    };
    
    (async() => {
      await initDb();
      await view();
    })();
    https: //stackoverflow.com/questions/69122670/no-index-exists-for-this-sort-couchdb#
    <script src="https://github.com/pouchdb/pouchdb/releases/download/7.1.1/pouchdb-7.1.1.min.js"></script>
    <script src="https://github.com/pouchdb/pouchdb/releases/download/7.1.1/pouchdb.memory.min.js"></script>
    <script src="https://github.com/pouchdb/pouchdb/releases/download/7.1.1/pouchdb.find.min.js"></script>
    <pre id='view'></pre>
    <div style='margin-top:2em'></div>
    <pre id='result'>
    </pre>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-12
      • 1970-01-01
      • 2018-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多