mongodb python使用条件更新子字段

2024-10-03 23:28:06 发布

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

在这上面坐了一会儿。我有数据库和字典。 db的结构如下:

    { 'label': 'Argiope',
  'uri': 'http://dbpedia.org/resource/Argiope_(spider)',
  'description': 'The genus Argiope includes rather large and spectacular spiders that often ...',
  'name': 'Argiope',
  'synonym': ["One", "Two"],
  'classification': {
                    'family': 'Orb-weaver spider',
                    'class': 'Arachnid',
                    'phylum': 'Arthropod',
                    'order': 'Spider',
                    'kingdom': 'Animal',
                    'genus': None
                    }
}

现在我要做的是,通过在classification字段中创建一个新条目来更新数据库。键总是相同的“binomialAuthority”和值,我的dict值。但是我只想更新label等于key的那些数据集(所以我只更新正确的数据集)

我从以下几点开始:

^{pr2}$

但这不起作用,仍然缺少状况。有人吗知道怎么做吗?在


Tags: 数据org数据库httpdb字典uri结构
1条回答
网友
1楼 · 发布于 2024-10-03 23:28:06

如果我正确理解您的问题,您的更新应该类似于:

db.arachnid.update({'label': 'binomialAuthority'}, {'$set': dict([('classification.' + key, value) for key, value in data.items()])}, multi=True)

编辑

再读一遍你的问题,也许你想要的是:

^{pr2}$

相关问题 更多 >