I have a json document like this
{
"_id": "73a43a134b4169f04ddb734ef09785e9",
"title": "US",
"areas":
[
{
"text": "xyz"
}
{
"text": "abc"
}
]
}
I need to search the document with areas = xyz
I have created design document like this, but not getting results. any idea ?
{
"_id": "_design/areaapp",
"filters": {
"by_area": "function(doc, req) { for( var i=0, l=doc.areas.length; i<l; i++) { return doc.areas[i].text === req.query.area; }}"
}
}
here is my link https://hostname/databse/_changes?filter=areaapp/by_area&area=xyz
Thanks
答案 0 :(得分:0)
将其排序
{
"_id": "_design/areaapp",
"filters": {
"by_area": "function(doc, req) { if (doc.areas) {if(doc.areas.length > 0) {for(var idx in doc.areas) {if(doc.areas[idx].text==req.query.area) {return true; }}}}}"
}
}
答案 1 :(得分:0)
嗯。这不是如何查询couchdb的方式.. 你需要写一个像
这样的视图 {
"_id": "_design/areaapp",
"_views": {
"by_area":{
"map": "function(doc, req) {
for( var i=0, l=doc.areas.length; i<l; i++) {
emit (doc.areas[i].text,1)
}
}"
}
}
}
这会将btree结果存储在couchdb中。
以后你可以像查询一样 ?/ _view / byArea键=&#34; XYZ&#34;
然后你得到文档的id作为结果.. 要么 ?/ _view / byArea键=&#34; XYZ&#34;&安培; include_docs =真 为了获得文档本身