从数据库删除6个对象中的1个后,PouchDB find()返回空数组

编程入门 行业动态 更新时间:2024-10-23 13:28:19
本文介绍了从数据库删除6个对象中的1个后,PouchDB find()返回空数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是我的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项:

  • 在查询时,只有根据选择器要求其所有索引字段必须存在时,才能使用索引。例如,如果您的索引包含字段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()返回空数组

    本文发布于:2023-10-30 04:33:46,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1541780.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:数组   象中   数据库   PouchDB   find

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!