PouchDB find() query stops working after modifying docs when using an index

邮差的信 提交于 2019-12-11 04:37:27

问题


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.


回答1:


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:


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>

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

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


来源:https://stackoverflow.com/questions/49785164/pouchdb-find-query-stops-working-after-modifying-docs-when-using-an-index

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!