从 Cloudant 中的文档中提取嵌套值
Extracting nested values from documents in Cloudant
我在提取存储在 Cloudant (CouchDB) 中的值时遇到问题。下面是一个文件存储的例子。
{
"_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
}
]
}
]
}
数据库包含大量文档,每个文档包含多个数据记录,由序号表示。每个文档的数据记录(序列)数可以是 1 ~ 无穷大之间的任何值。其中一个视图应显示一个数组,其中包含从与 John Doe 相关的所有文档中提取的所有值。目前该数据库仅包含与 John Doe 相关的文档,但这将在未来发生变化。
当前查看文档如下:
function(doc) {
if(doc.Records) {
doc.Records.forEach(function(SeqNo) {
emit(SeqNo,null);
});
}
}
这为每个数据记录提供了以下结果(每个序列的单独结果):
{
"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"
}
第一步是确保在构建数组之前可以提取所需的值。
emit(doc.SeqNo,null) 不只提供序号,emit(SeqNo,doc.SeqNo)也不提供
我的问题是,
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?
如有任何帮助,我们将不胜感激。
.forEach
正在返回个人记录,我认为此视图所需的 map
函数是
function(doc) {
if(doc.Records) {
doc.Records.forEach(function(Record) {
emit(Record.SeqNo, null);
});
}
}
我在提取存储在 Cloudant (CouchDB) 中的值时遇到问题。下面是一个文件存储的例子。
{
"_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
}
]
}
]
}
数据库包含大量文档,每个文档包含多个数据记录,由序号表示。每个文档的数据记录(序列)数可以是 1 ~ 无穷大之间的任何值。其中一个视图应显示一个数组,其中包含从与 John Doe 相关的所有文档中提取的所有值。目前该数据库仅包含与 John Doe 相关的文档,但这将在未来发生变化。
当前查看文档如下:
function(doc) {
if(doc.Records) {
doc.Records.forEach(function(SeqNo) {
emit(SeqNo,null);
});
}
}
这为每个数据记录提供了以下结果(每个序列的单独结果):
{
"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"
}
第一步是确保在构建数组之前可以提取所需的值。
emit(doc.SeqNo,null) 不只提供序号,emit(SeqNo,doc.SeqNo)也不提供 我的问题是,
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?
如有任何帮助,我们将不胜感激。
.forEach
正在返回个人记录,我认为此视图所需的 map
函数是
function(doc) {
if(doc.Records) {
doc.Records.forEach(function(Record) {
emit(Record.SeqNo, null);
});
}
}