从数据库删除6个对象中的1个后,PouchDB find()返回空数组 [英] PouchDB find() returns empty array after delete 1 of 6 objects from db
问题描述
这是我的db.find():
db.createIndex({
索引:{
字段:['username','date'],
名称:'usernameDateIndex',
ddoc: usernameDateIndex
}
})。then(()= > {
db.find({
选择器:{
用户名:'org.couchdb.user:'+名称
},
排序:[{用户名:'desc'},{date:'desc'}],
use_index:'usernameDateIndex'
})。then(result => {
//工作正常,我明白了预期
})。catch(错误=> {
});
})。catch(错误=> {
});
这可以正常工作并返回包含六个文档的数组。然后,我删除一个这样的文档:
doc._deleted = true;
db.put(
doc
).then(response => {
// db.allDocs()返回5个文档,这是正确的
// db .find()返回一个空数组
})。catch(error => {
});
删除后,db.AllDocs()正常工作,但db.createIndex和db.find相同()以上列出的返回和空数组。我还尝试了删除后没有db.createIndex()的db.find(),但是我仍然得到一个空数组,并且没有错误。
我丢失了在这里迈出一步?删除后是否需要进行某种提交?我是否需要删除索引并重新创建?我是否需要使用CouchDB远程服务器强制复制?不确定发生了什么。
最后一件事,这是一个React Native应用程序,它使用pouchdb-react-native和pouchdb-find包。它的后备存储是AsyncStorage。
在此答案,根据答案的第2项:
< ol start = 2>
在查询时,只有根据选择器要求其所有索引字段
必须存在时,才能使用索引。例如,如果您的
索引包含字段A和B,但您仅查询A,我们将不能使用
索引,因为它不会包含包含A的文档但不是B。
也许是您遇到问题的原因。
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.
这篇关于从数据库删除6个对象中的1个后,PouchDB find()返回空数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!