user1242003
user1242003

Reputation: 51

Couchdb view Queries

Could you please help me in creating a view. Below is the requirement

select * from personaccount where name="srini" and user="pup" order by lastloggedin

I have to send name and user as input to the view and the data should be sorted by lastloggedin.

Below is the view I have created but it is not working

{
    "language": "javascript",
    "views": {
        "sortdatetimefunc": {
            "map": "function(doc) {
                emit({
                    lastloggedin: doc.lastloggedin,
                    name:         doc.name,
                    user:         doc.user
                },doc);
            }"
        }
    }
}

And this the curl command iam using:

http://uta:password@localhost:5984/personaccount/_design/checkdatesorting/_view/sortdatetimefunc?key={\"name:srini\",\"user:pup\"}

My Questions are

As sorting will be done on key and I want it on lastloggedin so I have given that also in emit function.

But iam passing name and user only as parameters. Do we need to pass all the parameters which we give it in key?


First of all I want to convey to you for the reply, I have done the same and iam getting errors. Please help

Could you please try this on your PC, iam posting all the commands :

curl -X PUT http://uta:password@localhost:5984/person-data

curl -X PUT http://uta:password@localhost:5984/person-data/srini -d '{"Name":"SRINI", "Idnum":"383896", "Format":"NTSC", "Studio":"Disney", "Year":"2009", "Rating":"PG", "lastTimeOfCall": "2012-02-08T19:44:37+0100"}'

curl -X PUT http://uta:password@localhost:5984/person-data/raju -d '{"Name":"RAJU", "Idnum":"456787", "Format":"FAT", "Studio":"VFX", "Year":"2010", "Rating":"PG", "lastTimeOfCall": "2012-02-08T19:50:37+0100"}'

curl -X PUT http://uta:password@localhost:5984/person-data/vihar -d '{"Name":"BALA", "Idnum":"567876", "Format":"FAT32", "Studio":"YELL", "Year":"2011", "Rating":"PG", "lastTimeOfCall": "2012-02-08T19:55:37+0100"}'

Here's the view as you said I created :

{
   "_id": "_design/persondestwo",
   "_rev": "1-0d3b4857b8e6c9e47cc9af771c433571",
   "language": "javascript",
   "views": {
       "personviewtwo": {
           "map": "function (doc) {\u000a    emit([ doc.Name, doc.Idnum, doc.lastTimeOfCall ], null);\u000a}"
       }
   }
}


I have fired this command from curl command :

curl -X GET http://uta:password@localhost:5984/person-data/_design/persondestwo/_view/personviewtwo?startkey=["SRINI","383896"]&endkey=["SRINI","383896",{}]descending=true&include_docs=true

I got this error :

[4] 3000
curl: (3) [globbing] error: bad range specification after pos 99
[5] 1776
[6] 2736
[3]   Done                    descending=true
[4]   Done(3)                 curl -X GET http://uta:password@localhost:5984/person-data/_design/persondestwo/_view/personviewtwo?startkey=["SRINI","383896"]
[5]   Done                    endkey=["SRINI","383896"]

I am not knowing what this error is.

I have also tried passing the parameters the below way and it is not helping

curl -X GET http://uta:password@localhost:5984/person-data/_design/persondestwo/_view/personviewtwo?key={\"Name\":\"SRINI\",\"Idnum\": \"383896\"}&descending=true

But I get different errors on escape sequences

Overall I just want this query to be satisfied through the view :

select * from person-data where Name="SRINI" and Idnum="383896" orderby lastTimeOfCall

My concern is how to pass the multiple parameters from curl command as I get lot of errors if I do the above way.

Upvotes: 2

Views: 2841

Answers (2)

stj
stj

Reputation: 9107

I just stumbled upon this question. The errors you are getting are caused by not escaping this command:

curl -X GET http://uta:password@localhost:5984/person-data/_design/persondestwo/_view/personviewtwo?startkey=["SRINI","383896"]&endkey=["SRINI","383896",{}]descending=true&include_docs=true

The & character has a special meaning on the command-line and should be escaped when part of an actual parameter. So you should put quotes around the big URL, and escape the quotes inside it:

curl -X GET "http://uta:password@localhost:5984/person-data/_design/persondestwo/_view/personviewtwo?startkey=[\"SRINI\",\"383896\"]&endkey=[\"SRINI\",\"383896\",{}]descending=true&include_docs=true"

Upvotes: 1

Dominic Barnes
Dominic Barnes

Reputation: 28439

First off, you need to use an array as your key. I would use:

function (doc) {
    emit([ doc.name, doc.user, doc.lastLoggedIn ], null);
}

This basically outputs all the documents in order by name, then user, then lastLoggedIn. You can use the following URL to query.

/_design/checkdatesorting/_view/sortdatetimefunc?startkey=["srini","pup"]&endkey=["srini","pup",{}]&include_docs=true

Second, notice I did not output doc as the value of your query. It takes up much more disk space, especially if your documents are fairly large. Just use include_docs=true.

Lastly, refer to the CouchDB Wiki, it's pretty helpful.

Upvotes: 1

Related Questions