【发布时间】:2017-08-31 08:08:51
【问题描述】:
我正在尝试查询以下数据:
{
"post_code": 35801,
"country": "United States",
"country_abbreviation": "US",
"places": [
{
"place_name": "Huntsville",
"longitude": -86.5673,
"state": "Alabama",
"state_abbreviation": "AL",
"latitude": 34.7269
}
]
},
..........
我的问题:
- 如何在查询中首先包含地点名称。
以下 javascript 没有给出结果:
{
"selector": {
"post_code": {"$eq": 35801}
},
"fields":["places.placename"]
}
post_code 上的正确索引:
{
"index": {
"fields": [
"post_code"
]
},
"type": "json",
"name": "post-code-index"
}
我确实设置了一个附加索引,如下所示:
{
"index": {
"fields": [
"places.placename"
]
},
"type": "json"
}
-
如果数据有以下字段会怎样:
"old_post_code_numbers": [12345, 67890, ......]
或:
-
如果数据有以下字段会怎样:
"places": { "place_name": "Huntsville", "longitude": -86.5673, "state": "Alabama", "state_abbreviation": "AL", "latitude": 34.7269 }
JSON 有很多形式,我很难掌握这类查询所需的 javascript 背后的原理。 我知道这很简单。一定很简单。
非常感谢我应该/可以访问的任何指导或网站。
编辑:
我设法让以下内容在在线 javascript 测试站点上运行,但我仍然无法在 CouchDB Mango 中运行。这在 CouchDB 中肯定是可能的。???
var xxx=
{
"post_code": 82941,
"country": "United States",
"country_abbreviation": "US",
"places": [
{
"place_name": "Pinedale",
"longitude": -109.8561,
"state": "Wyoming",
"state_abbreviation": "WY",
"latitude": 42.8543
}]
}
console.log(xxx.places[0].place_name);
【问题讨论】:
-
您的请求的棘手之处在于您试图返回数组元素属性。因此,我认为 Cloudant/CouchDB 不支持它。为此,您可以使用 map/reduce。
-
嗨@Alexis。感谢您对地图减少的评论。我添加了一个编辑(上图),但可能无关紧要。
-
@Alexis 是的,map/reduce 与
places[0].place_name合作。谢谢。 -
嗨@Alexis。如果您有时间,请看一下我提出的答案。
标签: javascript json couchdb-mango couchdb-2.x