我正在尝试使用包含 –upsert 的 csv 和 Mongoimport 更新我集合中每个文档中的单个字段。 但是,该过程会删除文档中的所有其他字段。
我有一个 Books Collection,其中包含如下文件:
{
"_id" : "knOIv8ZUUK",
"Price" : 2.2,
"Title" : "Rats Ahoy"
}
{
"_id" : "okYEGuWznv",
"Price" : 3.3,
"Title" : "Friendly Fish"
}
一个csv文件:
_id,Price
knOIv8ZUUK,2.2
okYEGuWznv,3.3
并导入使用:
mongoimport --db local --collection Books --upsert --type csv
--headerline --file c:\import\newPrice
结果删除标题字段
{
"_id" : "knOIv8ZUUK",
"Price" : 2.2
}
{
"_id" : "okYEGuWznv",
"Price" : 3.3
}
我错误地认为 Upsert 只会更新导入的字段。 那么我可以使用另一个过程来更新大量文档中的 1 个字段吗? 谢谢
最佳答案
来自 mongoimport --upsertFields doc 您可能需要使用模式合并:
Merge existing documents that match a document in the import file with the new document. mongoimport will insert all other documents. Merge Matching Documents during Import describes how to use --mode merge.
并指定字段名,默认为'_id'
--mode merge --upsertFields <fieldname>
所以对于你的情况
--mode merge --upsertFields
关于MongoDB:使用带有 csv 的 MongoImport 仅更新单个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40701733/