Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 对于每个from列表,在mongo集合中插入项_Python_Arrays_Mongodb_Pymongo - Fatal编程技术网

Python 对于每个from列表,在mongo集合中插入项

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

我有一个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-%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")