Python 对于每个from列表,在mongo集合中插入项
我有一个ID列表Python 对于每个from列表,在mongo集合中插入项,python,arrays,mongodb,pymongo,Python,Arrays,Mongodb,Pymongo,我有一个ID列表arr=[123456765…] 我需要为以下每一项做一个测试: 我需要运行一系列步骤,比如从MongoDB集合中获取一些值,然后将其转换为列表,然后对返回的字段进行更改并将其插入到新集合中 如何为列表中的每个id执行此操作 以下是我正在运行的其他函数: hello=col.find({'id':123}, {'_id':0}) for item in hello: to_ISO = datetime.strptime(item['created_dt'], "%Y-%m
arr=[123456765…]
我需要为以下每一项做一个测试:
我需要运行一系列步骤,比如从MongoDB集合中获取一些值,然后将其转换为列表,然后对返回的字段进行更改并将其插入到新集合中
如何为列表中的每个id执行此操作
以下是我正在运行的其他函数:
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
,执行我需要的其余功能,然后插入记录,然后移动到列表上的下一项
我有什么办法可以做到这一点吗?很难确切地知道你想做什么,但是如果你知道确切的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")