with CouchDB is possible do queries "like" SQL. http://guide.couchdb.org/draft/cookbook.html says that

How you would do this in SQL:

SELECT field FROM table WHERE value="searchterm"

How you can do this in CouchDB:

Use case: get a result (which can be a record or set of records) associated with a key ("searchterm").

To look something up quickly, regardless of the storage mechanism, an index is needed. An index is a data structure optimized for quick search and retrieval. CouchDB’s map result is stored in such an index, which happens to be a B+ tree.

To look up a value by "searchterm", we need to put all values into the key of a view. All we need is a simple map function:

function(doc) {
  if(doc.value) {
    emit(doc.value, null);
  }
}

This creates a list of documents that have a value field sorted by the data in the value field. To find all the records that match "searchterm", we query the view and specify the search term as a query parameter:

/database/_design/application/_view/viewname?key="searchterm"

how can I do this with PouchDB? the API provide methods to create temp view, but how I can personalize the get request with key="searchterm"?

有帮助吗?

解决方案

You just add your attribute settings to the options object:

var searchterm = "boop";

db.query({map: function(doc) {
   if(doc.value) {
     emit(doc.value, null);
   }
 }, { key: searchterm }, function(err, res) { ... });

see http://pouchdb.com/api.html#query_database for more info

其他提示

using regex

    import PouchDB from 'pouchdb';
    import PouchDBFind from 'pouchdb-find';
    ...
    PouchDB.plugin(PouchDBFind)
    const db = new PouchDB(dbName); 
    db.createIndex({index: {fields: ['description']}})
    ....
    const {docs, warning} = await db.find({selector: { description: { $regex:   /OVO/}}})
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top