Reputation: 133
I have the following json (simplified) objects in a couchdb storage:
[{
"_id": "5ea7a53e670b432e0fe22a7bc10024db",
"_rev": "1-ae70c8906f7aa6d73539a89f7ad960ee",
"type": "job"
}, {
"_id": "5ea7a53e670b432e0fe22a7bc10041d9",
"_rev": "4-fa0ba68c35ca548b497a7309389f9087",
"type": "scan",
"job_id": "5ea7a53e670b432e0fe22a7bc10024db",
"number": 1
}, {
"_id": "5ea7a53e670b432e0fe22a7bc100520e",
"_rev": "4-3e6b1a028786c265ecb7362e245d049e",
"type": "scan",
"job_id": "5ea7a53e670b432e0fe22a7bc10024db",
"number": 2
}]
I want to make a post request with the keys ["5ea7a53e670b432e0fe22a7bc10024db", 2] (the job id and a scan number). How can I make a map function for a view to filter out the job that has the given id and the measurement that matches the job_id and the number?
Thanks, Radu
Upvotes: 1
Views: 1612
Reputation: 2854
What is you expected output of the request? If you just want to get the scan, emit in map
the key you want to search for:
function (doc) {
if (type == "scan" && number) {
emit([doc.job_id, doc. number], doc);
}
}
If you need two documents -- the job (full document, not just id
) and the scan, either emit both of them in single emit
with include_docs=true
parameter in the request's URL:
function (doc) {
if (doc.type == "scan" && doc.number) {
emit([doc.job_id, doc. number], {scan: doc, _id: doc.job_id});
}
}
or in two emit
's with:
function (doc) {
if (doc.type == "scan" && doc.number && doc.job_id) {
emit([doc.job_id, doc. number, "job"], {_id: doc.job_id);
emit([doc.job_id, doc. number, "scan"], {_id: doc._id});
}
}
You will get both document with startkey=["5ea7a53e670b432e0fe22a7bc10024db", 2]&endkey=["5ea7a53e670b432e0fe22a7bc10024db", 2, {}]&include_docs=true
(or keys=[]
) in the URL.
Check the view API for the include_docs
option.
Upvotes: 2