你能帮我创建一个 View 吗?以下是要求
select * from personaccount where name="srini" and user="pup" order by lastloggedin
我必须将名称和用户作为 View 的输入发送,并且数据应按laSTLoggedin排序。
下面是我创建的 View ,但它不起作用
{
"language": "javascript",
"views": {
"sortdatetimefunc": {
"map": "function(doc) {
emit({
lastloggedin: doc.lastloggedin,
name: doc.name,
user: doc.user
},doc);
}"
}
}
}
这是我使用的curl命令:
http://uta:password@localhost:5984/personaccount/_design/checkdatesorting/_view/sortdatetimefunc?key={\"name:srini\",\"user:pup\"}
我的问题是
由于排序将在 key 上完成,并且我希望在 LaSTLoggedin 上进行排序,因此我也在 emit 函数中给出了该排序。
但我仅将名称和用户作为参数传递。我们需要传递我们在 key 中给出的所有参数吗?
首先,我想向您转达答复,我已经做了同样的事情,但出现了错误。请帮忙
您能否在您的电脑上尝试一下,我发布了所有命令:
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"}'
这是您所说的我创建的 View :
{
"_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
但是我在转义序列上遇到了不同的错误
总的来说,我只是希望通过 View 满足此查询:
select * from person-data where Name="SRINI" and Idnum="383896" orderby lastTimeOfCall
我关心的是如何从curl命令传递多个参数,因为如果我这样做,我会遇到很多错误。
最佳答案
首先,您需要使用数组作为 key 。我会使用:
function (doc) {
emit([ doc.name, doc.user, doc.lastLoggedIn ], null);
}
这基本上按名称顺序输出所有文档,然后是用户,然后是最后登录。您可以使用以下网址进行查询。
/_design/checkdatesorting/_view/sortdatetimefunc?startkey=["srini","pup"]&endkey=["srini","pup",{}]&include_docs=true
其次,请注意我没有输出 doc
作为您的查询的值。它会占用更多的磁盘空间,尤其是当您的文档相当大时。只需使用 include_docs=true
。
最后,引用CouchDB Wiki ,这很有帮助。
关于view - Couchdb 查看查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9511976/