简体   繁体   中英

Issues returning specific object from MongoDB Nested JSON Array

currently writing a flask api with python/pymongo.

Currently I am unable to return a specific object from this nested json array. I have been utilizing pymongo's db.collection.find(), and I either return the entire array or an empty set. Same thing happens in MongoDB I will provide what I have tried below.

Sample of the JSON I am querying:

{
"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. Note trailName does nothing here, as I have opted to test with individual IDs and names until I return the specific object I want.

@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. I eiether return the entire array, not the specific object attributes.

One way to find your specific element :

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

Try it on mongoplayground.net .

try using:

return = list(users.find({}))

then tags = return("tags")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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