0

I'm trying to find the global result (calendar) by ID and find nested results in the calendar by another ID.

If I use find function - it's work for me (found exactly what I need)

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

But if I use update function - I get the error:

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

If I add upsert=True I get another error:

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

What I'm doing wrong? How to append new_results to founded calendar_results?

My data structure looks like that:

"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
         }
      ]
   }
]
Konstantin Rusanov
  • 6,414
  • 11
  • 42
  • 55
  • Does this help? https://stackoverflow.com/questions/5055797/pymongo-upsert-throws-upsert-must-be-an-instance-of-bool-error, `upsert` is the 3rd argument here, which is not the case in either of your attempts at calling update – Devesh Kumar Singh Dec 29 '19 at 17:53
  • I'm use it as the last argument self.calendar_data.update({'calendar_id': calendar_id}, {'calendar_results': {'$elemMatch': {'results_id': results_id}}}, {'$addToSet': {'calendar_results.$.results': new_results}}, upsert=True) And does not help – Konstantin Rusanov Dec 29 '19 at 17:55
  • take a look at the api of [update](https://api.mongodb.com/python/current/api/pymongo/collection.html#pymongo.collection.Collection.update) . Based on how you use it, it should be the third argument if using positionally, or stick with keyword arguments and specify accordingly – Devesh Kumar Singh Dec 29 '19 at 17:56
  • also as a side note it says `update` is deprecated in the docs and to use those other methods – gold_cy Dec 29 '19 at 17:57
  • With update_one I get another error (ValueError: update only works with $ operators) but I'm using $ operator – Konstantin Rusanov Dec 29 '19 at 17:59

1 Answers1

1

This solution works for me

calendar_data.update_one({'calendar_results': {'$elemMatch': {'results_id': results_id}}},
{'$push': {'calendar_results.$.results': new_results}})
Konstantin Rusanov
  • 6,414
  • 11
  • 42
  • 55