微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 mongoimports 向集合文档添加新字段

如何解决使用 mongoimports 向集合文档添加新字段

我根据文档的 csv 创建了一个包含 5 个文档的小型集合,其中包含 49 个字段。然后我做了一个由 3 个字段组成的多索引:日期、国家、省/州。在我的表格中,并非所有文档都有省/州,因此字段有时为空。然后我只需将新文档添加到具有此结构的集合中,一切正常。

集合中的示例文档:

_id:601eb064d50081040c1a06d8
date:"2021-02-02"
province/state:"AK"
confirmed:52775
probable_cases:NaN
negative:NaN
pending:NaN
total_test_results:1511785
hospitalized_currently:49
hospitalized_cumulative:1209
country:"US"

索引

[('date',1),('country',('province/state',1)]

然后到我导入的源文件,我进行聚合,添加新字段,其中有 60 个。现在我需要根据这个文档更新我的整个集合。对此,我有困难。

我最终想得到什么:

_id:601eb064d50081040c1a06d8
date:"2021-02-02"
province/state:"AK"
confirmed:52775
probable_cases:NaN
negative:NaN
pending:NaN
total_test_results:1511785
hospitalized_currently:49
hospitalized_cumulative:1209
country:"US"
intermediate_region_code:1
total_vaccinations:2
people_vaccinated:3

我正在使用 mongosnell 并且正在编写以下查询

mongoimport -c=update_test3 -d test_import --upsert --mode merge --upsertFields date_1_country_1_province/state_1 --type csv --file file2.csv --headerline

我收到错误 11000 - 重复索引。我可能对要传递的命令和参数感到困惑。如何将新字段添加到现有代码的集合中?

D:\Program\MongoDB\bin>mongoimport -c=update_test3 -d test_import --upsert --mode merge --upsertFields date_1_country_1_province/state_1 --type csv --file file2.csv --headerline
2021-02-06T19:25:50.333+0400    connected to: mongodb://localhost/
2021-02-06T19:25:50.370+0400    Failed: bulk write error: [{[{E11000 duplicate key error collection: test_import.update_test3 index: date_1_country_1_province/state_1 dup key: { date: "2021-02-02",country: "US",province/state: "AK" }}]},{<nil>}]
2021-02-06T19:25:50.370+0400    0 document(s) imported successfully. 1 document(s) Failed to import.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。