![](/img/trans.png)
[英]Retrieve data from a collection(mongo),modify the returned object and insert it into another mongo collection
[英]For each from list insert item in mongo collection
我有一個 id 列表arr = [123, 456, 765...]
我需要為其中的每一個做一個:
我需要運行一系列步驟,例如從 MongoDB 集合中獲取一些值,然后將其轉換為列表,然后更改返回的字段並將它們插入回新集合。
我如何為該列表中的 forEach 執行此操作。
這是我正在運行的功能的 rest:
hello=col.find({'id':123}, {'_id':0})
for item in hello:
to_ISO = datetime.strptime(item['created_dt'], "%Y-%m-%dT%H:%M:%S.%f%z")
list_audits = list(audit_pages_res)
pprint(list_audits)
db.newcol.insert({date: to_ISO})
我想從arr
做一個 forEach ,替換hello
中的id
做我需要的功能的 rest 然后插入記錄,然后移動到列表中的下一項。
我有什么辦法嗎?
很難確切地弄清楚您要做什么,但是如果您知道確切的 _id 則無需使用查找和循環; 只需使用find_one()
。 從...開始:
arr = [123, 456, 765]
for arr_id in arr:
hello=col.find_one({'id':arr_id}, {'_id':0})
if hello is not None: # Need to check it found a record
to_ISO = datetime.strptime(hello['created_dt'], "%Y-%m-%dT%H:%M:%S.%f%z")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.