Search code examples
javascriptcouchdbcouchdb-futon

How to find documents by startsWith() in Futon


I am working with CouchDB and I have to find documents whose name start with "5463"
The document structure I have is :

{_id: "018bdd61897af56d0b3c421d4dfb1a92", _rev: "1-b37c710c91450b93510f547194631aa0", type: "active_matter", id: 177, name: "3009/TR02", ismaster: true}

So in Futon I am trying something like :

function(doc) {
    if (doc.ismaster == true &&  (doc.name).startsWith("5463")){  
        emit([doc.type], doc);
    }
}

but not results are getted. What I am doing wrong?


Solution

  • It seems like CouchDB doesn't support the startsWith method. Instead use indexOf, with a little conditional you can use it the same way.

    Using your Example:

    function(doc){
      if(doc.ismaster && (doc.name.indexOf('5463') == 0)){
        emit([doc.type], doc);
      }
    }
    

    That will work the same way as startsWith.