Pregunta

Could anyone provide me with a great explination or tutorial around a user management couchbase database.

With SQL login would be

SELECT * FROM users WHERE email = :email AND password = :password

How would this work in couchbase, assuming my user document looks like this

{
    "uid": "3",
    "email": "dummy@example.com",
    "password": "6a1644c781989cb3f47c8a38a0e75c6c",
    "name" : "John Doe",
    "jsonType" : "user"
}

My View at the moment is

function (doc, meta) {
    if(doc.jsonType == "user"){
        emit([doc.email, doc.password], [doc]);
    }
}

And I can goto

http://localhost:8092/default/_design/dev_users/_view/login?stale=false&connection_timeout=60000&limit=10&skip=0&key=[%22dummy@example.com%22,%226a1644c781989cb3f47c8a38a0e75c6c%22]

which returns

{
    "total_rows":3,"rows":
    [{
        "id":"user::3",
        "key":["dummy@example.com","6a1644c781989cb3f47c8a38a0e75c6c"],
        "value":
        [{
            "uid":"3",
            "email":"dummy@example.com",
            "password":"6a1644c781989cb3f47c8a38a0e75c6c",
            "name" : "John Doe",
            "jsonType" : "user"
        }]
    }]
}

I would just like advice on if I am approaching this the correct way. I am very new to couchbase. I have googled this but can't seem to find exactly what I'm looking for. Any help appreciated to get me off to a good start.

¿Fue útil?

Solución

In noSQL(key/value) you have to pay special attention to the KEY structure design. In your case you should identify what is your loginID? Is it "email" field? If so, you can create a key based on that. e.g. KEY: "dummy@example.com" VALUE:

So then when user enters user id/password you can simply call one GET operation from the couchbase. If no such loginID exists, you will not get any json value. If loginID exists, then you will get back json document that you can use to check password and also (in case password matches) populate user specific data from that json in your login session. NOTE: I assume you are not storing clear passwords in json, but instead using password digest with salt.

So no view functionality required here.

As for views usage, I highly recommend reading though "Basic Couchbase querying for SQL people"

also read on Creating an e-commerce platform with Couchbase 2.0

Otros consejos

I would suggest not including the whole document in the index - i.e. change your emit to something like:

emit([doc.email, doc.password])

This will minimise the size of (and hence time taken to operate on) the index. If you later need the actual doc contents you can use a normal get operation to fetch it, using the id field of the query row. Some of the SDKs provide a method to perform this for you, for example setIncludeDocs() in the Java SDK.

Other than that this looks pretty reasonable. A detailed overview of Views is included in the Couchbase developer guide. Another good resource for complete example applications (above the standard getting started tutorials) is: http://couchbasemodels.com

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top