我在couchDB中有两种文件,其中包含以下json类型:
1.
{
"_id": "4a91f3e8-616a-431d-8199-ace00055763d",
"_rev": "2-9105188217acd506251c98cd4566e788",
"Vehicle": {
"type": "STRING",
"name": "Vehicle",
"value": "12345"
},
"Start": {
"type": "DATE",
"name": "Start",
"value": "2014-09-10T11:19:00.000Z"
}
}
2.
{
"_id": "4a91f3e8-616a-431d-8199-ace00055763d",
"_rev": "2-9105188217acd506251c98cd4566e788",
"Equipment": {
"type": "STRING",
"name": "Equipment",
"value": "12345"
},
"Start": {
"type": "DATE",
"name": "Start",
"value": "2014-09-10T11:19:00.000Z"
}
}
我想创建一个查看所有这些doc.Vehicle.value = 12345或doc.Equipment.value = 12345的文档的视图。 如何使此视图返回所有这些类型的文档。 提前谢谢。
答案 0 :(得分:5)
在您的视图中,只发出两个(是的,地图函数可能会为同一个doc发出多次不同的键值):
function(doc){
if (doc.Equipment) {
emit(doc.Equipment.value, null)
}
if (doc.Vehicle) {
emit(doc.Vehicle.value, null)
}
}
用相同的密钥请求它们:
http://localhost:5984/db/_design/ddoc/_view/by_equip_value?key="12345"
有关CouchDB视图的更多信息,另请参阅Guide to Views。
答案 1 :(得分:0)
使用Kxepals版本,您无法查询结果类型(" 12345"可以是Vehicle,OR Equipment)。你只能在使用" include_docs = true"时才能看到结果。并在文档中搜索,或使用结果的id进行第二次查询。
如果要查看类型(或按类型查询),则需要扩展视图: ..
if(doc.Equipment) {
emit (doc.Equipment.value,doc.Equipment.name);
}
if(doc.Vehicle) {
emit(doc.Vehicle.value,doc.Vehicle.name);
}
此处,名称是结果行的值。
但是,如果将名称作为第一个查询项,则还可以在查询中定义结果:
if(doc.Equipment) {
emit([doc.Equipment.name,doc.Equipment.value],null);
}
if(doc.Vehicle) {
emit ([doc.Vehicle.name,doc.Vehicle.value],null);
}
在这里, 您的车辆查询:
/viewname?startkey=["Vehicle"]&Endkey=["Vehicle",{}]
设备:
/viewname?startkey=["Equipment"]&endkey=["Equipment,{}]
这里,名称是结果行键数组的第一个项目。
也许这会有所帮助:http://de.slideshare.net/okurow/couchdb-mapreduce-13321353
BTW:更好的解决方案是:{
"_id": "4a91f3e8-616a-431d-8199-ace00055763d",
"_rev": "2-9105188217acd506251c98cd4566e788",
"type": "Vehicle",
"value":"12345",
"Start": {
"type": "DATE",
"name": "Start", // ? maybe also obsolete, because already inside "Start" Element
"value": "2014-09-10T11:19:00.000Z"
}
}
{
"_id": "4a91f3e8-616a-431d-8199-ace00055763d",
"_rev": "2-9105188217acd506251c98cd4566e788",
"type": "Equipment",
"value":"12345",
"Start": {
"type": "DATE",
"name": "Start", // ? maybe also obsolete, because already inside "Start" Element
"value": "2014-09-10T11:19:00.000Z"
}
}
在这种情况下,您只能使用一个发射:
emit([doc.type,doc.value],null)