3

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.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Warren
  • 735
  • 7
  • 19
  • This issue happens if i change any existing field in the doc I put(). It is not just related to setting _deleted = true. I now believe this is by design and I just don't have an option needed to return all of my valid docs. – Warren Apr 09 '18 at 00:23
  • As [explained here](https://pouchdb.com/guides/mango-queries.html "Mango queries"), you can try the `explain` API of PouchDB to debug: `db.explain({ selector: { //... } }) .then(explained => { // detailed explained info can be viewed });` – Megidd Apr 09 '18 at 02:11
  • I have used explain, and both the find() that works and the one that doesn't are explained exactly the same and are using the correct indexes and correct selectors and sorts. – Warren Apr 09 '18 at 12:19

1 Answers1

-1

Take a loot at this answer, according to item 2 of the answer:

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

Megidd
  • 7,089
  • 6
  • 65
  • 142
  • Thanks for catching that. I changed the selector to include both fields. It still does not work. Seems to happen after I try to mark _deleted = true on one doc. – Warren Apr 08 '18 at 22:58
  • 2
    The consequences of the circumstances in this answer would be a slower query (by not using the index), not a failed/empty result set. – Jonathan Hall Apr 10 '18 at 08:21