MongoDB更新与Pymongo不起作用

发布于 2025-02-06 01:57:40 字数 884 浏览 2 评论 0原文

我想在MongoDB Docuemnt中的现有对象中添加新键,我正在尝试更新具有更新查询的特定示例,但我在数据库中没有看到新键。

我有一个这样的对象:

{'_id': 'patent_1023', 
'raw': {'id': 'CN-109897889-A',
 'title': 'A kind of LAMP(ring mediated isothermal amplification) product visible detection method', 
'assignee': '北京天恩泽基因科技有限公司', 
'inventor/author': '徐堤',
 'priority_date': '2019-04-17', 
'filing/creation_date': '2019-04-17',
 'publication_date': '2019-06-18', 
'grant_date': None, 
'result_link': 'https://patents.google.com/patent/CN109897889A/en', 'representative_figure_link': None
}, 
'source': 'Google Patent'}

我在RAW中添加了两个新键,并希望仅使用新键“抽象”和“描述”更新“原始”,

这是我所做的。

d = client.find_one({'_id': {'$in': ids}})

d['raw'].update(missing_data) # missing_data contain new keys to be added in raw.

here = client.find_one_and_update({'_id': d['_id']}, {'$set': {"raw": d['raw']}})

I wanted to add new keys to an existing object in a MongoDB docuemnt, I am trying to update the specific abject with update query but I don't see new keys in database.

I have a object like this:

{'_id': 'patent_1023', 
'raw': {'id': 'CN-109897889-A',
 'title': 'A kind of LAMP(ring mediated isothermal amplification) product visible detection method', 
'assignee': '北京天恩泽基因科技有限公司', 
'inventor/author': '徐堤',
 'priority_date': '2019-04-17', 
'filing/creation_date': '2019-04-17',
 'publication_date': '2019-06-18', 
'grant_date': None, 
'result_link': 'https://patents.google.com/patent/CN109897889A/en', 'representative_figure_link': None
}, 
'source': 'Google Patent'}

I added two new keys in raw and want to update only 'raw' with new keys 'abstract' and 'description'

Here is what I have done.

d = client.find_one({'_id': {'$in': ids}})

d['raw'].update(missing_data) # missing_data contain new keys to be added in raw.

here = client.find_one_and_update({'_id': d['_id']}, {'$set': {"raw": d['raw']}})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

她如夕阳 2025-02-13 01:57:40

update_oneupdate_many都可以使用以下方式:

missing_data = {'abstract':'a book', 'description':'a fun book'};

ids = [ 'patent_1023', 'X'];

rc=db.foo.update_one(
    {'_id': {'$in': ids}},

    # Use pipeline form of update to exploit richer agg framework
    # function like $mergeObjects.  Below we are saying "take the 
    # incoming raw object, overlay the missing_data object on top of
    # it, and then set that back into raw and save":
    [ {'$set': {
        'raw': {'$mergeObjects': [ '$ROOT.raw', missing_data ] }
      }}
    ]
)

Both update_one and update_many will work with this:

missing_data = {'abstract':'a book', 'description':'a fun book'};

ids = [ 'patent_1023', 'X'];

rc=db.foo.update_one(
    {'_id': {'$in': ids}},

    # Use pipeline form of update to exploit richer agg framework
    # function like $mergeObjects.  Below we are saying "take the 
    # incoming raw object, overlay the missing_data object on top of
    # it, and then set that back into raw and save":
    [ {'$set': {
        'raw': {'$mergeObjects': [ '$ROOT.raw', missing_data ] }
      }}
    ]
)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文