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.