Couchdb视图查询

时间:2012-03-01 07:03:15

标签: view couchdb

你能帮我创建一个视图吗?以下是要求

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

我必须将名称和用户作为输入发送到视图,数据应按lastloggedin排序。

以下是我创建的视图,但它无法正常工作

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

这是curl命令iam使用:

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

我的问题是

由于排序将在key上完成,我想在lastloggedin上完成,所以我也在emit函数中给出了它。

但是iam仅将名称和用户作为参数传递。我们是否需要传递我们在密钥中提供的所有参数?


首先,我想传达给你答复,我已经做了同样的事情,但我得到了错误。请帮忙

你可以在你的电脑上试试这个,我发布了所有的命令:

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"}'

以下是您创建的视图:

{
   "_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

我收到了这个错误:

[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"]

我不知道这个错误是什么。

我也尝试过以下方式传递参数,但没有帮助

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

但是我在转义序列上得到了不同的错误

总的来说,我只想通过视图来满足此查询:

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

我担心如何从curl命令传递多个参数,因为如果我按上述方式进行操作会遇到很多错误。

2 个答案:

答案 0 :(得分:1)

首先,您需要使用数组作为密钥。我会用:

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

这基本上按名称顺序输出所有文档,然后是用户,然后是lastLoggedIn。您可以使用以下URL进行查询。

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

其次,请注意我没有输出doc作为查询的值。它占用了更多的磁盘空间,特别是如果您的文档相当大。只需使用include_docs=true

最后,请参阅CouchDB Wiki,它非常有帮助。

答案 1 :(得分:1)

我只是偶然发现了这个问题。您获得的错误是由于没有转义此命令引起的:

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

& character在命令行中具有特殊含义,并且应该在实际参数的一部分时进行转义。 因此,您应该在大URL周围加上引号,并转义其中的引号:

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"