laung
laung

Reputation: 11

How to do a convert a simple common sql query from: Converting MYSQL Query to Couchbase NoSQL?

The sample that I got from this couchbase blog, seems to suggest that I need to loop the search in the nosql which in my opinion is inefficient and dangerous when it comes to searching millions of document. Did I misundertand it? Any suggestion?

So how do I do it in couchbase nosql language the right way?

query example :

select * from users where loginname='xxx' and passwd='yyy'

Nosql/couchbase example :

function (doc, meta) {
  if (doc.ingredients) {
    for (i=0; i < doc.user.length; i++)                                                              {                                                                                                         if (doc.user[i].loginname != null)
      {
        emit([doc.user[i].loginname , doc.loginname], null);  
      }
    }  
  }
}

Upvotes: 1

Views: 574

Answers (2)

Tug Grall
Tug Grall

Reputation: 3520

One way of achieving this, in Couchbase 2.x, will be to emit the login and password in the index and do an exact key search. Since I do not know the structure of your JSON document I will just considerate that the username and password are 2 attributes of your document (doc parameter)

function (doc, meta) {
    emit([doc.loginname , doc.password], null);  
}  

Then you can search for the document using the following query (I am using the REST API you will have to do it using the client SDK of you choice.

?key=["username_to_check","password_to_check"]

Note: do not hesitate to add more information about the structure of your document(s) to help the STOF users to answer the question properly.


Developer Preview

If you are an technology early adopter I am inviting you to look at the next generation query language that Couchbase is building: http://www.couchbase.com/communities/n1ql

In this case the query will look like

SELECT *
    FROM contact-bucket 
    WHERE loginname = "username"
    AND password = "password"

Upvotes: 1

Vertical Jo
Vertical Jo

Reputation: 43

I am not sure if your example is a real case one, or just to understand how CB is working. But I think you should try to avoid using view in this case.

It seems to be a login search you are doing. If this the purpose of your query, you can indeed emit username and password like this:

function (doc, meta) {
    emit([doc.loginname , doc.password], null);  
} 

First if you are using view, you should not emit the password, just the loginname.

function (doc, meta) {
    emit([doc.loginname], null);  
}

And verify the password manually in your application.

But at the end, you should actually not use any view at all. What you should do is having the key of your document being username (or something deterministic) and just perform a simple GET.

View is great, but really heavy for your CB. You should first try to see if you can not model your schema so you do not have to use them, and then, if stuck, create a view.

Upvotes: 0

Related Questions