简体   繁体   English

对于 mongo 集合中的每个 from 列表插入项

[英]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.

相关问题 从collection(mongo)检索数据,修改返回的对象并将其插入另一个mongo collection - Retrieve data from a collection(mongo),modify the returned object and insert it into another mongo collection 如何使用列表中的每个项目仅循环一次列表以在​​Python中的另一个列表的开头插入? - How to loop through a list using each item from a list only once to insert at the beginning of another list in Python? 一次从每个列表中返回 1 项 - Return 1 item from each list at a time 将一个列表中的每个项目添加到另一个列表中的每个项目到第三个列表 - Adding each item from one list to each item from another list to third list 从mongo集合中提取数据 - Extracting data from a mongo collection 如何在Python中将列表中的每个项目附加到列表中的列表? - How to append each item from a list to a list in a list in Python? 如何将列表中的每个项目添加到另一个列表中的字典? - How to add each item from a list to dictionaries from another list? 列表理解 - 连接每个子列表中的第 n 个项目 - List comprehension - concatenate nth item from each sub-list 尝试将每个项目的部分从列表中拉到新列表中 - Trying to pull parts of each item from a list into a new list 如何在 csv 的单元格中列出列表中的每个项目? - How can I list each item from list in a cell in csv?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM