问题描述
我的文档他们有这样的结构:
I have Documents they have this structure:
{id: ####,
rev: ####,
"Cam_name": "Camera SX",
"colour": "white",
"manufacturer": "Sony",
"rec_limit": 180,
"Customer": ["Mike","Ann","James"]
}
{id: ####,
rev: ####,
"Cam_name": "PXSV CAM",
"colour": "white",
"manufacturer": "LG",
"rec_limit": 144,
"Customer": ["Mike","Oliver","Mr. Rain"]
}
{id: ####,
rev: ####,
"Cam_name": "LxSV Double",
"colour": "white",
"manufacturer": "Phillips",
"rec_limit": 160,
"Customer": ["Mike"]
}
和我想打一个地图功能查询,在那里我可以看到哪些客户麦克使用所有Cam_Names。
And i want to make an MAP Function query where i can see ALL Cam_Names which the Customer Mike is using.
我有一个呈三角地图显示功能,但这仅显示Cam_Name LxSV双,只有客户迈克。我想显示哪些迈克是使用所有Cam_Names。
i have a simillar Map Function but this shows only the Cam_Name LxSV Double and only the Customer Mike. i want to show all Cam_Names which mike is using.
更改为MyQuery:
MyQuery:
function(doc){
if(doc.Customer == "Mike"){
emit(doc.Cam_name, doc.Customer)
该查询给了我不正确的结果。
This query gives me not the right result.
推荐答案
如果您的查询看起来的究竟的那样,那么你有一个语法错误。但同时, doc.Customer
是一个数组,所以你不能做一个简单的平等检查。
If your query looks exactly like that, then you have a syntax error. But also, doc.Customer
is an array, so you can't do a simple equality check.
但检查值的存在,在一个阵列是完全不必要的,你的地图功能可以简单地是这样的:
But checking the existence of a value in an array is totally unnecessary, your map function can simply look like this:
function (doc) {
doc.Customer.forEach(function (customer) {
emit(customer, doc.Cam_name);
});
}
然后,查询与 / {DB} / _设计/ {ddoc} / _视图/ {查看}您的看法呢?关键=迈克
您输出如下:
{
"total_rows": 3,
"offset": 0,
"rows": [
{
"id": "####",
"key": "Mike",
"value": "Camera SX"
},
{
"id": "####",
"key": "Mike",
"value": "PXSV CAM"
},
{
"id": "####",
"key": "Mike",
"value": "LxSV Double"
}
]
}
现在,您可以使用相同的视角以寻找任何客户,不只是你在你的地图功能指定的任何人。
Now, you can use this same view to find any customer, not just whomever you specify in your map function.
这篇关于查找文档阵列的CouchDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!