开发者

Select users in couchdb

I have an HTML form for authentication of users, with SQL language is easier to extract data

select name, password from users where name='nameField' and password='passwordField'

In couchdb I cant use local views just temp views:

curl -X POST -H 'Content-Type: application/json' -d '{"map": "function (doc) {if (doc.name === "nameField" && doc.password === "passwordField") {emit (doc.name, doc.passWord)}}"}' http://localhost:5984/somedb/_temp_view
开发者_JAVA技巧

But it isnt recommended (Click here), what should I do ? :(

Thanks


This is how I usually like to do it...

  1. Create your design doc and view. Ex., /_design/users/_views/byUserPass

  2. Your map function might look something like this (no reduce function):

    function(doc)
    {
      if(doc.docType == "user")
        emit([doc.username, doc.password], doc);
    }

  3. Then I can query like this: http://localhost:5984/somedb/_design/users/_views/byUserPass?key=["exampleUsername", "examplePassword"]

  4. If I get a returned row, then the credentials were correct. As a bonus, I also get all of the user's information. Now I can update their session (ex., put their user ID into the session) or the document (ex., update "when last logged on") without having to make an additional call to CouchDB for their info.

If you don't want to update anything, then return a value of null: emit([doc.username, doc.password], null); - this will reduce bandwidth consumption as well, as you're no longer passing the whole doc back.

Cheers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜