I am facing problem using couchdb. I am using nano module for this in nodejs. How can I implement search like match for user name and password. I tried this
body.rows.forEach(function(row) {
if(row.doc._id==user_id && row.doc.password==password){
found = true;
data = row;
}
});
but this is slow process
Your code
body.rows.forEach ...
is a map function (it iterates over every row) which executes a filter functionif(row.doc._id==user_id ...
to a row. Thats what CouchDB views do - exactly the same.True. Because of that CouchDB creates an index (a B-Tree that is a file inside CouchDB's database directory) and keeps this index up-to-date. The performance advantage for every request is that the result will be taken from the already prepared index instead of a just-in-time calculation as in your example.
The CouchDB view map function could look like:
The key of every row is
[:username,:password]
and the value isnull
. If you requestyou will get the row immediately. Append a
&include_docs=true
and you will also get the whole doc appended to the row (alternatively you could emit a partial of the doc as value instead ofnull
)handling user accounts in CouchDB
User accounts and especially passwords are confidential data. CouchDB has the built-in
_users
db for it. I will not go into the details of the access control specifics of that db but want to say Store user account data there!. If you need account data outside this db then interpret that docs as "public profiles" e.g. to let users discover and connect to each other.