【发布时间】:2019-12-29 17:39:59
【问题描述】:
我正在尝试通过 ID 查找全局结果(日历)并通过另一个 ID 在日历中查找嵌套结果。
如果我使用 find 功能 - 它对我有用(找到我需要的东西)
calendar_data.find({'calendar_id': calendar_id}, {'calendar_results': {'$elemMatch': {'results_id': results_id}}})
但如果我使用更新功能 - 我会收到错误:
calendar_data.update({'calendar_id': calendar_id},
{'calendar_results': {'$elemMatch': {'results_id': results_id}}},
{'$addToSet': {'calendar_results.$.results': new_results}})
TypeError: upsert 必须是 True 或 False
如果我添加 upsert=True 我会收到另一个错误:
TypeError: update() 为参数 'upsert' 获取了多个值
我做错了什么?如何将 new_results 附加到已创建的 calendar_results?
我的数据结构是这样的:
"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
}
]
}
]
【问题讨论】:
-
这有帮助吗? stackoverflow.com/questions/5055797/…,
upsert是这里的第三个参数,在您尝试调用 update 时都不是这种情况 -
我用它作为最后一个参数 self.calendar_data.update({'calendar_id': calendar_id}, {'calendar_results': {'$elemMatch': {'results_id': results_id}} }, {'$addToSet': {'calendar_results.$.results': new_results}}, upsert=True) 并没有帮助
-
看看update的api。根据你的使用方式,如果使用位置,它应该是第三个参数,或者坚持关键字参数并相应地指定
-
作为旁注,它说
update在文档中已弃用并使用其他方法 -
使用 update_one 我得到另一个错误(ValueError:更新仅适用于 $ 运算符)但我正在使用 $ 运算符
标签: python arrays json mongodb pymongo