简体   繁体   English

从 MongoDB 嵌套 JSON 数组返回特定 object 的问题

[英]Issues returning specific object from MongoDB Nested JSON Array

currently writing a flask api with python/pymongo.目前正在用 python/pymongo 编写 flask api。

Currently I am unable to return a specific object from this nested json array.目前我无法从这个嵌套的 json 数组返回特定的 object。 I have been utilizing pymongo's db.collection.find(), and I either return the entire array or an empty set.我一直在使用 pymongo 的 db.collection.find(),我要么返回整个数组,要么返回一个空集。 Same thing happens in MongoDB I will provide what I have tried below.同样的事情发生在 MongoDB 我将提供我在下面尝试过的内容。

Sample of the JSON I am querying:我查询的JSON的样本:

{
"elements": [
    {
        "type": "item",
        "id": 1,
        "geometry": {
            "type": "LineString",
            "coordinates": [
              
            ]
        },
        "tags": {
            "foot": "yes",
            "highway": "path",
            "mtb": "yes",
            "name": "Arch Cape to Cape Falcon Trail",
            "sac_scale": "hiking",
            "surface": "ground",
            "_osm_type": "way"
        }
    },
    {
        "type": "item",
        "id": 2,
        "geometry": {
            "type": "LineString",
            "coordinates": [
              
            ]
        },
        "tags": {
            "foot": "yes",
            "highway": "path",
            "mtb": "yes",
            "name": "Cape Falcon Trail",
            "sac_scale": "hiking",
            "surface": "ground",
            "_osm_type": "way"
        }
    }
]}

I removed what was in coordinates for readability, due to it being thousands of lines long.为了便于阅读,我删除了坐标中的内容,因为它有数千行长。

here is the python code.这是 python 代码。 Note trailName does nothing here, as I have opted to test with individual IDs and names until I return the specific object I want.注意 trailName 在这里什么都不做,因为我选择了使用个人 ID 和名称进行测试,直到我返回我想要的特定 object。

@app.route('/<trailName>', methods = ['GET'])
def trail(trailName):
    try:
        # trails = db.output_data_retry.find({'elements.tags.name':"Arch Cape to Cape Falcon Trail"}) # returns entire collection
        # trails = db.output_data_retry.find_one({ "elements" : { "$elemMatch" : { "id":1 } } } ) # also just returns entire collection
        # trails = db.output_data_retry.aggregate({ "elements" : { "$elemMatch" : { "id":1 } } } ) # {"error": "pipeline must be a list"}
        # trails = db.output_data_retry.find({ "elements.tags" : { "$elemMatch" : { "name":"Arch Cape to Cape Falcon Trail" } } } ) # returns empty set
        # trails = db.output_data_retry.find({ "elements" : { "tags" : { "$elemMatch" : { "name":"Arch Cape to Cape Falcon Trail" } } }  } ) # empty set
        # trails = db.output_data_retry.find({ "elements.tags" : { "$elemMatch" : { "name":"Arch%20Cape%20to%20Cape%20Falcon%20Trail" } } } ) # empty set
        # trails = db.output_data_retry.find({ "elements.tags" : { "$elemMatch" : { "name":'Arch%20Cape%20to%20Cape%20Falcon%20Trail'} } } ) # empty set
        # trails = db.output_data_retry.find({ 'tags' : { "$elemMatch" : { 'name':"Arch Cape to Cape Falcon Trail" } } } ) # empty set
        # trails = db.output_data_retry.find({ "$match" : { "elements.tags" : { "$elemMatch" : { "name":"Arch Cape to Cape Falcon Trail" } } } } ) # empty set
        # trails = db.output_data_retry.find_one({'elements.tags.name':"Arch Cape to Cape Falcon Trail"}) # returns entire collection
        # trails = db.output_data_retry.find_one({'elements.tags.name':['Arch Cape to Cape Falcon Trail']}) #returns null
        # trails = db.output_data_retry.find_one({'elements.id':[1]}) #returns null
        trails = db.output_data_retry.find({'elements.id':[1]}) #returns an empty set
        return dumps(trails) 
    except Exception as e:
       return dumps({'error' : str(e)})

also note that the same thing happens in MongoDB compass when I attempt these queries.另请注意,当我尝试这些查询时,同样的事情发生在 MongoDB 指南针中。 I eiether return the entire array, not the specific object attributes.我要么返回整个数组,而不是特定的 object 属性。

One way to find your specific element : find特定element的一种方法:

db.collection.find({
  "elements.tags.name": "Arch Cape to Cape Falcon Trail"
},
{
  "elements.$": 1,
  "_id": 0
})

Try it on mongoplayground.net .mongoplayground.net上试用。

try using:尝试使用:

return = list(users.find({}))返回 = 列表(用户。查找({}))

then tags = return("tags")然后标签=返回(“标签”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM