在ouchdb中选择用户 [英] Select users in couchdb
问题描述
我有一个用于用户身份验证的HTML表单,使用SQL语言更容易提取数据
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'
在ouchdb中,我不能仅使用临时视图来使用本地视图:
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 ? :(
谢谢
推荐答案
这是我通常喜欢这样做的方式...
This is how I usually like to do it...
-
创建您的设计文档和视图.例如,/_ design/users/_views/byUserPass
Create your design doc and view. Ex., /_design/users/_views/byUserPass
您的地图函数可能看起来像这样(没有reduce函数):
Your map function might look something like this (no reduce function):
功能(文档)
{
if(doc.docType ==用户")
emit([doc.username,doc.password],doc);
}
function(doc)
{
if(doc.docType == "user")
emit([doc.username, doc.password], doc);
}
然后我可以这样查询:http://localhost:5984/somedb/_design/users/_views/byUserPass?key=["exampleUsername", "examplePassword"]
Then I can query like this: http://localhost:5984/somedb/_design/users/_views/byUserPass?key=["exampleUsername", "examplePassword"]
如果我得到返回的行,则凭据是正确的.作为奖励,我还获得了所有用户信息.现在,我可以更新他们的会话(例如,将他们的用户ID放入会话中)或文档(例如,更新上次登录时"),而不必额外致电CouchDB以获得他们的信息.
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.
如果您不想更新任何内容,请返回null值:emit([doc.username, doc.password], null);
-这也将减少带宽消耗,因为您不再需要传递整个文档.
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.
干杯.
这篇关于在ouchdb中选择用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!