Reputation: 25
I having issues extracting values stored in Cloudant (CouchDB). Below an example of a document stored.
{
"_id": "d47cd130de736894be5c464c314f1083",
"_rev": "1-2baf765d6f78bd80b4a604718063c0eb",
"Name": John,
"Surname": "Doe",
"ID": "89884600000001936983",
"Records": [
{
"SeqNo": 14776,
"Reason": 25,
"DateUTC": "2015-12-01 01:59:35",
"Fields": [
{
"HeartRate": "68",
"Weight": "75",
},
{
"Eyesight": 20,
},
{
"OtherData": {
"1": 4217,
"2": 1179,
"3": 2588,
"4": 2488,
"5": 21
},
"FType": 6
}
]
},
{
"SeqNo": 14777,
"Reason": 25,
"DateUTC": "2015-12-05 02:00:35",
"Fields": [
{
"HeartRate": "72",
"Weight": "78",
},
{
"Eyesight": 20,
},
{
"OtherData": {
"1": 4217,
"2": 1198,
"3": 2588,
"4": 2488,
"5": 21
},
"FType": 6
}
]
}
]
}
The database contains numerous documents and each document contains multiple data records denoted by the sequence number. The number of data records (sequences) per document can be anything from 1 ~ infinity. One of the views should display an array containing all the values extracted from all of the documents relating to John Doe. Currently the database only contains documents relating to John Doe, but this will change in the future.
The current view document looks as follow:
function(doc) {
if(doc.Records) {
doc.Records.forEach(function(SeqNo) {
emit(SeqNo,null);
});
}
}
This provides the following result for each data record (separate result for each sequence):
{
"id": "d47cd130de736894be5c464c314f1083",
"key": {
"SeqNo": 14776,
"Reason": 25,
"DateUTC": "2015-12-01 01:59:35",
"Fields": [
{
"HeartRate": "68",
"Weight": "75",
},
{
"Eyesight": 20,
},
{
"OtherData": {
"1": 4217,
"2": 1179,
"3": 2588,
"4": 2488,
"5": 21
},
"FType": 6
}
]
},
"value": null,
"_id": "d47cd130ce736894be5c464c314f1083"
}
The first step would be to make sure the required values can be extracted before constructing the array.
emit(doc.SeqNo,null) does not provide the sequence number only, nor does emit(SeqNo,doc.SeqNo) My questions are,
how do I extract the sequence number only?
how do I then build an array from the results?
would this change when I want to extract OtherData.1 value?
Any help would be appreciated greatly.
Upvotes: 0
Views: 581
Reputation: 151
The .forEach
is returning the individual Record, I think the map
function you need for this view is
function(doc) {
if(doc.Records) {
doc.Records.forEach(function(Record) {
emit(Record.SeqNo, null);
});
}
}
Upvotes: 2