PouchDB find() returns empty array after delete 1 of 6 objects from db

笑着哭i 提交于 2019-12-02 00:10:25

问题


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.


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



来源:https://stackoverflow.com/questions/49719678/pouchdb-find-returns-empty-array-after-delete-1-of-6-objects-from-db

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