[英]For each from list insert item in mongo collection
I have a list of ids arr = [123, 456, 765...]
我有一个 id 列表
arr = [123, 456, 765...]
I need to do a for each of these:我需要为其中的每一个做一个:
I need to run a bunch of steps like grabbing some values from a MongoDB collection, then converting that into a list, and then making a change to the returned fields and inserting them back to a new collection.我需要运行一系列步骤,例如从 MongoDB 集合中获取一些值,然后将其转换为列表,然后更改返回的字段并将它们插入回新集合。
How do I do this for a forEach of the id's in that list.我如何为该列表中的 forEach 执行此操作。
Here is the rest of functions I am running:这是我正在运行的功能的 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})
I want to do a forEach from arr
, replace the id
in hello
do the rest of the functions I need and then insert the record and then move to the next item on the list.我想从
arr
做一个 forEach ,替换hello
中的id
做我需要的功能的 rest 然后插入记录,然后移动到列表中的下一项。
Is there any way for me to do that?我有什么办法吗?
Hard to figure out exactly what you're trying to do but if you know the exact _id then no need to use a find and a loop;很难确切地弄清楚您要做什么,但是如果您知道确切的 _id 则无需使用查找和循环; just use
find_one()
.只需使用
find_one()
。 start with:从...开始:
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.