MongoDB(pymongo)查找并推送到嵌套数组

2024-05-29 02:55:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试按ID查找全局结果(日历),并按另一个ID查找日历中的嵌套结果

如果我使用find函数-它对我有用(正好找到了我需要的东西)

calendar_data.find({'calendar_id': calendar_id}, {'calendar_results': {'$elemMatch': {'results_id': results_id}}})

但是如果我使用update函数-我会得到错误:

calendar_data.update({'calendar_id': calendar_id},
                     {'calendar_results': {'$elemMatch': {'results_id': results_id}}},
                     {'$addToSet': {'calendar_results.$.results': new_results}})

TypeError: upsert must be True or False

如果我加上upsert=True我会得到另一个错误:

TypeError: update() got multiple values for argument 'upsert'

我做错了什么?如何将新的\u结果附加到已创建的日历\u结果?你知道吗

我的数据结构如下:

"calendar_results":[ 
   { 
      "checkin":"2020-01-18",
      "checkout":"2020-01-19",
      "results_id":"2a2f3199-98b6-439d-8d5f-bdd6b34b0fd7",
      "guests_number":0,
      "pets_allowed":0,
      "days_count":1,
      "query":"My Query",
      "results":[ 
         { 
            "id":5345345,
            "name":"My name",
            "reviews_count":5,
            "avg_rating":5.0,
            "rate_per_night":75.0,
            "cleaning_fee":10.0,
            "service_fee":0,
            "price_per_night":75.0,
            "total_price":85.0,
            "checkin":"2020-01-18",
            "checkout":"2020-01-19",
            "query":"Test",
            "position":1
         },
         { 
            "id":312312312,
            "name":"Some name",
            "reviews_count":111,
            "avg_rating":4.93,
            "rate_per_night":57.0,
            "cleaning_fee":7.0,
            "service_fee":0,
            "price_per_night":57.0,
            "total_price":64.0,
            "checkin":"2020-01-18",
            "checkout":"2020-01-19",
            "query":"Test",
            "position":2
         }
      ]
   }
]

Tags: nameidcountupdatequerypriceresultscalendar

热门问题