如何在 PouchDB 上查询

How to query on PouchDB

假设我的 pouchdb 中有这样的文档:

{
    "_id" : "685866641",
    "name" : "john",
    "age" : 56,
    "job" : "mason"
}

我如何查询数据库以 return job 是 mason 的所有文档? (我正在使用离子)

我不知道 "ionic" 但我一直在使用 "pouchdb.find.js":

https://github.com/nolanlawson/pouchdb-find

还有"pouchdb.quick-search.js":

https://github.com/pouchdb-community/pouchdb-quick-search

正如 Bill 所说,您需要一个插件。一旦你安装了 pouchdb,我就使用这些导入(我想我使用 6.3.2):

// pouchdb stuff:
import PouchDB from 'pouchdb';
import PouchDBFind from 'pouchdb-find';

那么你需要确保在构造函数中初始化这个插件:

 constructor(
    public http: HttpClient, 
  ) {
    PouchDB.plugin(PouchDBFind);
  }

以正常方式初始化数据库后:

initdb(dbname) { this.db = new PouchDB(dbname) }

您可以像这样使用查找查询:

getDataById = (snippetId) => {
      return new Promise((resolve, reject)=>{
          this.db.find({
              selector: {
                  _id: snippetId
              }
          }).then((res) => {
              resolve(res.docs[0]);
          }).catch((err) => { 
              reject(err);
          })
      })
  };