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
         }
      ]
   }
]
5
  • Does this help? stackoverflow.com/questions/5055797/…, upsert is the 3rd argument here, which is not the case in either of your attempts at calling update Commented Dec 29, 2019 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 Commented Dec 29, 2019 at 17:55
  • take a look at the api of update . Based on how you use it, it should be the third argument if using positionally, or stick with keyword arguments and specify accordingly Commented Dec 29, 2019 at 17:56
  • also as a side note it says update is deprecated in the docs and to use those other methods Commented Dec 29, 2019 at 17:57
  • With update_one I get another error (ValueError: update only works with $ operators) but I'm using $ operator Commented Dec 29, 2019 at 17:59

1 Answer 1

1

This solution works for me

calendar_data.update_one({'calendar_results': {'$elemMatch': {'results_id': results_id}}},
{'$push': {'calendar_results.$.results': new_results}})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.