mongodb count_documents给出错误:AttributeError:'dict'对象没有属性'_txn_read_preference'

flmtquvp  于 2023-11-17  发布在  Go
关注(0)|答案(1)|浏览(263)

我写了一个函数来检查一行是否存在,如果不存在就插入,如果存在就更新。所以我尝试使用count_documents。我使用一个名为filetype的字段作为我的唯一键。
示例文档如下所示:

  1. {
  2. "_id": ObjectId("652d47a64732d257ee31e846"),
  3. "filetype": "USTreasuryBillRates",
  4. "last_date_processed": "2023-10-16T00:00:00.000Z",
  5. "update_date": "2023-10-16T10:13:45.000Z"
  6. }

字符串
Python函数:

  1. def upsert_load_history(arg_file_type, arg_date, arg_db_collection):
  2. # insert if not there, otherwise update in place
  3. print("===== upsert_load_history ======")
  4. json_query = {"filetype": arg_file_type}
  5. row_count = arg_db_collection.count_documents(json_query, {'limit': 1})
  6. if row_count == 0:
  7. insert_load_history_new_rows(arg_file_type, arg_date, arg_db_collection)
  8. else:
  9. update_load_history_for_file_type(arg_file_type, arg_date, arg_db_collection)


错误代码:

  1. ===== upsert_load_history ======
  2. Traceback (most recent call last):
  3. File "C:\GitHub\ClientName\Apps\LambdaUSTreasury\SetupLoadHistoryDB.py", line 134, in <module>
  4. upsert_load_history(file_type, file_date, db_collection_filehistory)
  5. File "C:\GitHub\ClientName\Apps\LambdaUSTreasury\SetupLoadHistoryDB.py", line 61, in upsert_load_history
  6. row_count = arg_db_collection.count_documents(json_query, {'limit': 1})
  7. File "C:\GitHub\ClientName\Apps\LambdaUSTreasury\pymongo\collection.py", line 1786, in count_documents
  8. _cmd, self._read_preference_for(session), session)
  9. File "C:\GitHub\ClientName\Apps\LambdaUSTreasury\pymongo\common.py", line 870, in _read_preference_for
  10. return session._txn_read_preference() or self.__read_preference
  11. AttributeError: 'dict' object has no attribute '_txn_read_preference'

nfs0ujit

nfs0ujit1#

基于JonRSharpe的评论,这是我更正的代码。基本上将“limit”:1}改为limit=1。

  1. def upsert_load_history(arg_file_type, arg_date, arg_db_collection):
  2. # insert if not there, otherwise update in place
  3. print("===== upsert_load_history ======")
  4. update_date = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.000Z")
  5. json_query = {"filetype": arg_file_type}
  6. row_count = arg_db_collection.count_documents(json_query, limit=1)
  7. if row_count == 0:
  8. insert_load_history_new_rows(arg_file_type, arg_date, arg_db_collection)
  9. else:
  10. update_load_history_for_file_type(arg_file_type, arg_date, arg_db_collection)

字符串
此外,根据Belly Buster的评论,我已经发现了“Upsert”功能。下面我编写了一个新方法来测试它,它工作得很好。将“upsert=True”参数添加到update_one方法的调用中。如果不存在,它将插入,如果存在,则更新。

  1. def update2_load_history_for_file_type(arg_file_type, arg_date, arg_db_collection):
  2. # do update without retrieve first, using the upsert feature
  3. print("===== update_load_history_for_file_type ======")
  4. # Note, in my collection, filetype is my unique key
  5. json_query = {'filetype': arg_file_type}
  6. update_date = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.000Z")
  7. json_update = {
  8. '$set': {
  9. 'last_date_processed': arg_date,
  10. 'update_date': update_date
  11. }
  12. }
  13. print("json_query:")
  14. pprint.pprint(json_query)
  15. print("json_update:")
  16. pprint.pprint(json_update)
  17. result = arg_db_collection.update_one(json_query, json_update, upsert=True)
  18. print("Update result.matched_count=", result.matched_count)

展开查看全部

相关问题