簡體   English   中英

從 MongoDB 嵌套 JSON 數組返回特定 object 的問題

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

目前正在用 python/pymongo 編寫 flask api。

目前我無法從這個嵌套的 json 數組返回特定的 object。 我一直在使用 pymongo 的 db.collection.find(),我要么返回整個數組,要么返回一個空集。 同樣的事情發生在 MongoDB 我將提供我在下面嘗試過的內容。

我查詢的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"
        }
    }
]}

為了便於閱讀,我刪除了坐標中的內容,因為它有數千行長。

這是 python 代碼。 注意 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)})

另請注意,當我嘗試這些查詢時,同樣的事情發生在 MongoDB 指南針中。 我要么返回整個數組,而不是特定的 object 屬性。

find特定element的一種方法:

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

mongoplayground.net上試用。

嘗試使用:

返回 = 列表(用戶。查找({}))

然后標簽=返回(“標簽”)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM