使用索引修改文档后,PouchDB find()查询停止工作 [英] PouchDB find() query stops working after modifying docs when using an index

查看:56
本文介绍了使用索引修改文档后,PouchDB find()查询停止工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在React Native v0.46.2项目中使用本地PouchDB实例。我创建一个数据库,向其中添加记录,执行Mango查询(搜索具有特定事件ID的文档),该查询将按预期工作。然后,我修改一条记录,执行与以前相同的查询,结果不正确。似乎只是返回我刚刚修改的文档,而没有其他应返回的文档。如果以后再打印所有文档,则会显示所有文档,因为它们应包含新的修改。

I'm using a local PouchDB instance in a React Native v0.46.2 project. I create a database, add records to it, perform a Mango query (search for docs with a specific event ID), and the query works as expected. I then modify one record, perform the same query as before, and the results are not correct. It seems to just return the document I just modified, but none of the other documents that should be returned. If I print all docs afterwards, it shows all the documents as they should be including the new modification.

我关闭了索引,查询工作了。但是我确实需要索引才能对大量数据执行快速搜索。

I've turned off the index, and the queries work. But I do need the indexes in order to perform quick searches over large amounts of data.

为什么修改文档后查询无法正常工作?

Why do my queries stop working correctly after modifying a doc?

我创建了以下示例数据代码来演示该问题。我按此顺序执行以下操作:

I've created the sample data code below to demonstrate the issue. I do the following actions in this order:


  • 创建索引

  • 使用空白查询的索引数据库

  • 将批量文档添加到数据库

  • 执行查询

  • 修改文档

  • 再次执行查询。

  • Create index
  • index db with blank query
  • add bulkdocs to db
  • perform query
  • modify doc
  • perform query again.

`

testFunction() {

//Dummy data

let mockData = [{
'_id': '1',
'event_id': '136471',
},
{
'_id': '2',
'event_id': '136471',
},
{
'_id': '3',
'event_id': '136471',
},
{
'_id': '4',
'event_id': '136472',
}];

    //Create DB    
    this.testDB = new PouchDB('DBTest');

    let thisTestDB = this.testDB;

    this.testDB.createIndex({
        index: {
            fields: ['event_id'],
            ddoc: 'testTicketsDesignDoc',
        },
    })
    .then(
        (result) => {
            console.log('Indexing for Mango complete');

            //Index mango with initial blank query
            thisTestDB.find({
                selector: {},
                use_index: 'testTicketsDesignDoc',
                limit: 0,
            })
            .then(
                (result) => {
                    console.log('Indexing with blank Mango query complete');

                    //Add docs to database
                    thisTestDB.bulkDocs(mockData)
                    .then(
                        (result) => {
                            console.log('Bulkdocs successfully added to database.');

                            //Perform 1st query before modifying docs
                            thisTestDB.find({
                                selector: {
                                    'event_id': '136471',
                                },
                                use_index: 'testTicketsDesignDoc',
                            })
                            .then(
                                (searchResult) => {
                                    console.log('1st Mango query complete');
                                    console.log(searchResult);

                                    //Now modify a doc
                                    thisTestDB.get('1')
                                    .then(
                                        (doc) => {
                                            //Make any modifications to doc here

                                            thisTestDB.put(doc)
                                            .then (
                                                (putResult) => {
                                                    console.log(`Modifying doc successful: ${JSON.stringify(putResult)}`);

                                                    //Perform second query after modifying docs
                                                    thisTestDB.find({
                                                        selector: {
                                                            'event_id': '136471',
                                                        },
                                                        use_index: 'testTicketsDesignDoc',
                                                    })
                                                    .then(
                                                        (searchResult) => {
                                                            console.log('2nd Mango query complete');
                                                            console.log(searchResult);
                                                        }
                                                    );
                                                }
                                            )
                                            .catch(
                                                (error) => {
                                                    console.log(`Error modifying doc: ${error}`);
                                                }
                                            );
                                        }
                                    )
                                    .catch(
                                        (error) => {
                                            console.log(`Error modifying doc: ${error}`);
                                        }
                                    );
                                }
                            )
                            .catch(
                                (error) => {
                                    console.log(`Error performing first query: ${error.message}`);
                                }
                            );
                        }
                    )
                    .catch(
                        (error) => {
                            console.log(`Error adding bulk docs: ${error}`);
                        }
                    );
                }
            )
            .catch(
                (error) => {
                    console.log(`Error performing initial indexing query: ${error}`);
                }
            );
        }
    )
    .catch(
        (err) => {
            console.log(`Error - ${JSON.stringify(err)}`);
        }
    );
}

此外,除了修改文档外,我还尝试了删除文档然后进行压缩,然后放入新的文档副本。在索引搜索中,我仍然遇到相同的问题。

Also, instead of modifying a doc, I've also tried deleting the doc and compacting, then putting a new copy of the doc in. I still run into the same problems searching over the index.

推荐答案

我试图通过创建一个包含您的JavaScript代码的 index.html 文件和一个 so.js 文件来重现您的问题状况(稍加修改),并且我发现第一个查询和第二个查询都按预期运行,没有任何问题,控制台日志如下所示。注意第二个查询返回一个带有 _rev: 2-d36 ... 的文档,这是修改后的文档:

I tried to reproduce your problem conditions by creating an index.html file and a so.js file containing your JavaScript code (with little modification), and I observe that 1st and 2nd queries both work as expected without any problem, the console log are shown below. Notice the second query returns a doc with a _rev: "2-d36..." which is the modified doc:

只需仔细检查一下,这是我的 index.html 文件:

Just to double-check, here is my index.html file:

<html>
    <head>
        <meta charset="utf-8"/>
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
        <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
        <title>Pouch Debug</title>
    </head>
    <body>
        <script src="//cdn.jsdelivr.net/npm/pouchdb@6.4.3/dist/pouchdb.min.js"></script>
        <script src='//unpkg.com/pouchdb@6.4.3/dist/pouchdb.find.js'></script>
        <script src='./so.js'></script>
    </body>
</html>

我从 index.html 文件由Python 3.5如下所示:

I run my HTTP server from within the directory of my index.html file by Python 3.5 like below:

$ python3.5 -m http.server
Serving HTTP on 0.0.0.0 port 8000 ...

这是我的 so .js 文件,该文件在我的 index.html 文件中使用。我对您的代码进行了很少的更改:

Here is my so.js file, which is used inside my index.html file. I made very little changes to your code:

testFunction();

function testFunction() {

    //Dummy data

    let mockData = [{
'_id': '1',
'event_id': '136471',
    },
        {
'_id': '2',
'event_id': '136471',
        },
        {
'_id': '3',
'event_id': '136471',
        },
        {
'_id': '4',
'event_id': '136472',
        }];

    //Create DB    
    //this.testDB = new PouchDB('DBTest');

    let thisTestDB = new PouchDB('DBTest') /*this.testDB;*/

    thisTestDB.createIndex({
        index: {
            fields: ['event_id'],
            ddoc: 'testTicketsDesignDoc',
        },
    })
        .then(
            (result) => {
                console.log('Indexing for Mango complete');

                //Index mango with initial blank query
                thisTestDB.find({
                    selector: {},
                    use_index: 'testTicketsDesignDoc',
                    limit: 0,
                })
                    .then(
                        (result) => {
                            console.log('Indexing with blank Mango query complete');

                            //Add docs to database
                            thisTestDB.bulkDocs(mockData)
                                .then(
                                    (result) => {
                                        console.log('Bulkdocs successfully added to database.');

                                        //Perform 1st query before modifying docs
                                        thisTestDB.find({
                                            selector: {
                                                'event_id': '136471',
                                            },
                                            use_index: 'testTicketsDesignDoc',
                                        })
                                            .then(
                                                (searchResult) => {
                                                    console.log('1st Mango query complete');
                                                    console.log(searchResult);

                                                    //Now modify a doc
                                                    thisTestDB.get('1')
                                                        .then(
                                                            (doc) => {
                                                                //Make any modifications to doc here

                                                                thisTestDB.put(doc)
                                                                    .then (
                                                                        (putResult) => {
                                                                            console.log(`Modifying doc successful: ${JSON.stringify(putResult)}`);

                                                                            //Perform second query after modifying docs
                                                                            thisTestDB.find({
                                                                                selector: {
                                                                                    'event_id': '136471',
                                                                                },
                                                                                use_index: 'testTicketsDesignDoc',
                                                                            })
                                                                                .then(
                                                                                    (searchResult) => {
                                                                                        console.log('2nd Mango query complete');
                                                                                        console.log(searchResult);
                                                                                    }
                                                                                );
                                                                        }
                                                                    )
                                                                    .catch(
                                                                        (error) => {
                                                                            console.log(`Error modifying doc: ${error}`);
                                                                        }
                                                                    );
                                                            }
                                                        )
                                                        .catch(
                                                            (error) => {
                                                                console.log(`Error modifying doc: ${error}`);
                                                            }
                                                        );
                                                }
                                            )
                                            .catch(
                                                (error) => {
                                                    console.log(`Error performing first query: ${error.message}`);
                                                }
                                            );
                                    }
                                )
                                .catch(
                                    (error) => {
                                        console.log(`Error adding bulk docs: ${error}`);
                                    }
                                );
                        }
                    )
                    .catch(
                        (error) => {
                            console.log(`Error performing initial indexing query: ${error}`);
                        }
                    );
            }
        )
        .catch(
            (err) => {
                console.log(`Error - ${JSON.stringify(err)}`);
            }
        );
}

这篇关于使用索引修改文档后,PouchDB find()查询停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆