Here is my db.find():
db.createIndex({
index: {
fields: ['username', 'date'],
name: 'usernameDateIndex',
ddoc: "usernameDateIndex"
}
}).then(() => {
db.find({
selector: {
username: 'org.couchdb.user:' + name
},
sort: [{username: 'desc'}, {date:'desc'}],
use_index: 'usernameDateIndex'
}).then(result => {
// Works fine. I get what is expected
}).catch(error => {
});
}).catch(error => {
});
This works fine and returns an array with six docs. I then delete one doc like this:
doc._deleted = true;
db.put(
doc
).then(response => {
// db.allDocs() returns 5 docs this is correct
// db.find() returns an empty array
}).catch(error => {
});
After the delete, db.AllDocs() works correctly but the same db.createIndex and db.find() listed above returns and empty array. I have also tried db.find() after the delete without the db.createIndex() and I still get an empty array and I get no errors.
Am I missing a step here? Do I need to do some sort of commit after the delete? Do I need to delete the index and create it again? Do I need to force replication with my CouchDB remote server? Not sure what is going on.
One last thing, this is a React Native app that is using pouchdb-react-native and pouchdb-find packages. It's backing storage is AsyncStorage.
Take a loot at this answer, according to item 2 of the answer:
- At query time, an index can only be used if all it's indexed fields are required to exist according to the selector. For example, if your index contains fields A and B but you only query for A, we can't use the index because it won't include documents that contain A but not B.
Maybe that's the cause of your issue.
来源:https://stackoverflow.com/questions/49719678/pouchdb-find-returns-empty-array-after-delete-1-of-6-objects-from-db