【问题标题】:PouchDB find() query stops working after modifying docs when using an indexPouchDB find() 查询在使用索引时修改文档后停止工作
【发布时间】:2018-04-12 14:17:00
【问题描述】:

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

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

为什么我的查询在修改文档后停止正常工作?

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

  • 创建索引
  • 带有空白查询的索引数据库
  • 将 bulkdocs 添加到 db
  • 执行查询
  • 修改文档
  • 再次执行查询。

`

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)}`);
        }
    );
}

另外,我没有修改文档,而是尝试删除文档并压缩,然后放入文档的新副本。我在搜索索引时仍然遇到同样的问题。

【问题讨论】:

    标签: pouchdb


    【解决方案1】:

    我试图通过创建一个 index.html 文件和一个包含您的 JavaScript 代码(几乎没有修改)的 so.js 文件来重现您的问题条件,我观察到 第一个和第二个查询都按预期工作,没有任何问题,控制台日志如下所示。请注意,第二个查询返回一个带有 _rev: "2-d36..." 的文档,这是修改后的文档:


    请仔细检查,这是我的index.html 文件:

    <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>
    

    我使用 Python 3.5 从 index.html 文件的目录中运行我的 HTTP 服务器,如下所示:

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

    这是我的so.js 文件,在我的index.html 文件中使用。我对你的代码做了很少的改动:

    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)}`);
                }
            );
    }
    

    【讨论】:

    • 您的测试是在具有不同后备存储的浏览器环境中进行的。这个问题是在使用 AsynchStorage 作为后备存储的 React Native 环境中进行的。我想知道这是否相关。我在 React Native 应用程序中遇到了同样的问题。在此处查看我的问题“PouchDB find() 在从 db 中删除 6 个对象中的 1 个后返回空数组”
    • @Warren 我觉得your question 可能是相关的。您能否继续在PouchDB GitHub 上发布一个问题,参考您的问题以及这个问题?
    • @user3405291 感谢您复制此内容。我正在使用 React Native,我已经尝试使用 AsyncStorage 作为后备存储以及 SQLite。两者都表现出相同的问题。当我查看 allDocs 时,我也确实得到修改后文档的修订号增加。
    • @user3405291@Josh 我在 PouchDB GitHub #7219 上有一个问题。我会发布更详细的解释。
    猜你喜欢
    • 2019-08-21
    • 1970-01-01
    • 1970-01-01
    • 2021-12-10
    • 2016-06-12
    • 2018-04-27
    • 2011-06-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多