Select users in couchdb

让人想犯罪 __ 提交于 2019-12-21 05:37:09

问题


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

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

Thanks


回答1:


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.



来源:https://stackoverflow.com/questions/2769716/select-users-in-couchdb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!