在 CouchDB 中查找具有视图的子项
Finding a subkey with a view in CouchDB
我如何着手创建一个视图,使我能够根据带有主文档的 JSON 对象搜索特定值?
例如,下面是我数据库中的一个文档。
Phone 号码保存在 "phone" 中,它位于 "patientinfo" 键内。
如何使用此布局搜索 phone 号码?
{
"_id": "x2484",
"_rev": "2-44ac5e42ce95fcbcc8a743a256a3f6ce",
"patientinfo": {
"name": "Douglas",
"laststat": "Complete",
"timetocall": 1438034383,
"queue": 7,
"notes": "Spouse placed call",
"laststatcode": 501,
"timestamp": 1438023283,
"phone": 3141592653,
"attempts": 1
},
"call": {
"1": {
"timetocall": 1438034383,
"status": "Complete",
"laststat": "Complete",
"timestamp": 1438023283,
"lastintv": "moor",
"laststatcode": 501
}
}
}
原来答案比我想象的简单多了。我需要做的就是添加下一级 json 对象作为我的 if 和 emit 语句的一部分。
function(doc) {
if (doc.patientinfo.phone)
{
emit(doc.patientinfo.phone, doc);
}
}
我如何着手创建一个视图,使我能够根据带有主文档的 JSON 对象搜索特定值?
例如,下面是我数据库中的一个文档。 Phone 号码保存在 "phone" 中,它位于 "patientinfo" 键内。
如何使用此布局搜索 phone 号码?
{
"_id": "x2484",
"_rev": "2-44ac5e42ce95fcbcc8a743a256a3f6ce",
"patientinfo": {
"name": "Douglas",
"laststat": "Complete",
"timetocall": 1438034383,
"queue": 7,
"notes": "Spouse placed call",
"laststatcode": 501,
"timestamp": 1438023283,
"phone": 3141592653,
"attempts": 1
},
"call": {
"1": {
"timetocall": 1438034383,
"status": "Complete",
"laststat": "Complete",
"timestamp": 1438023283,
"lastintv": "moor",
"laststatcode": 501
}
}
}
原来答案比我想象的简单多了。我需要做的就是添加下一级 json 对象作为我的 if 和 emit 语句的一部分。
function(doc) {
if (doc.patientinfo.phone)
{
emit(doc.patientinfo.phone, doc);
}
}