2

I am having some trouble using the nano wrapper for couchDB within NodeJS:

https://github.com/dscape/nano

My question is, how do I find and return all users whose email matches 'foobar@baz.com?'. What if I only wish to return the name field?

I am confused by the concenpt of views and designnames/viewnames and if someone could show me an example call, that would be wonderful.

Thanks,

var nano = require('nano')('http://localhost:5984');
nano.db.create('users');
var users = nano.db.use('users');

user.views()//what parameters go in here?

/*example user object
user = {
  'id' : '123',
  'email' : 'foobar@baz.com'
  'name':'John Doe'
}
*/
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
ejang
  • 3,982
  • 8
  • 44
  • 70

1 Answers1

3

Looks like you need to create the view in couchdb before accessing it with nano. Views are attached to design documents as map (and optionally) reduce functions.

See this for more info: http://wiki.apache.org/couchdb/HTTP_view_API and this: http://guide.couchdb.org/editions/1/en/views.html

Your Design document for your users database might look something like the following:

{
  "_id":"_design/usersddoc",
  "_rev":"12345",
  "language": "javascript",
  "views":
  {
    "byEmail": {
      "map": "function(doc) { if (doc.email)  emit(null, doc.name) }"
    }
  }
}

It's important to understand the basics of how CouchDB works before using a library, otherwise you will be all kinds of confused. CouchDB has a fairly simple RESTful JSON interface, I recommend reading the Definitive Guide and browsing the Wiki to get a good idea of how it works.

  • yeah, I went back and read more carefully. I had to visit the Admin console and manually create a design document, which was very different than conventional MongoDB-style queries. – ejang Nov 11 '12 at 06:08
  • I agree CouchDB is very different from MongoDB, and Futon (the CouchDB admin interface at localhost:5984/_utils) doesn't really have great support for modifying design documents. I typically use CouchApp for that purpose as it's a little easier to maintain that way: http://couchapp.org/page/index – ellipse-of-uncertainty Nov 11 '12 at 06:11
  • Kanso is an alternative to CouchApp (http://kan.so), and there's actually a few others if you dig around the web. – Costa Michailidis Nov 16 '12 at 16:50