使用 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 (将#修改为@)