向集合pymong添加文档

2024-03-29 07:41:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我收集了这样的文件:

{'_id': ObjectId('5a66f98facb83b71b36aa44a'), 'UID': '', 'profile': {'firstName': '', 'lastName': '', 'email': ''},  'status': 'ACT'}

但是现在我想向这个集合添加一个其他文档的列表(在python中,我有一个dicts列表->;datilist),以便:

  • 如果索引字段与已经存在的字段匹配,则更新现有字段值,并最终添加新的缺少的键/值对
  • 如果与索引字段值不匹配,则添加到集合

我用这个语句创建了一个索引

db.utenti.createIndex( { "profile.email": 1 }, { unique: true } )

为了避免重复。你知道吗

我尝试过这样的表演:

utenti.update_many({},datilist.$[],upsert=True)

但很明显我缺少了一些东西。。我觉得这应该是Mongodb的一条线,我是新手。你知道吗


Tags: 文件文档id列表uidemailstatusfirstname