mongoimport upsert 创建新文档

mongimport upsert creates new documents

当我尝试像这样用 upsertFields 执行 mongoimport 时:

> mongoimport --db upsert-test --collection data --type tsv --headerline --file upsert-data.tsv --upsertFields MyCustomUpsertField -vvv
2018-10-10T15:08:39.358+0200    using upsert fields: [MyCustomUpsertField]
2018-10-10T15:08:39.424+0200    using 8 decoding workers
2018-10-10T15:08:39.424+0200    using 1 insert workers
2018-10-10T15:08:39.425+0200    will listen for SIGTERM, SIGINT, and SIGKILL
2018-10-10T15:08:39.425+0200    filesize: 61 bytes
2018-10-10T15:08:39.426+0200    using fields: "MyCustomUpsertField","SomeData"
2018-10-10T15:08:39.431+0200    connected to: localhost
2018-10-10T15:08:39.431+0200    ns: upsert-test.data
2018-10-10T15:08:39.431+0200    connected to node type: standalone
2018-10-10T15:08:39.432+0200    standalone server: setting write concern w to 1
2018-10-10T15:08:39.432+0200    using write concern: w='1', j=false, fsync=false, wtimeout=0
2018-10-10T15:08:39.432+0200    standalone server: setting write concern w to 1
2018-10-10T15:08:39.432+0200    using write concern: w='1', j=false, fsync=false, wtimeout=0
2018-10-10T15:08:39.433+0200    got line: ["Upsert-ID-1" "SomeData1"]
2018-10-10T15:08:39.433+0200    imported 1 document

然后再次执行相同的命令,结果是两个数据完全相同的文档。

添加(据说已过时的)--mode upsert 标志不会改变任何内容。始终创建新文档。

我的印象是,upserFields 会使用 MyCustomUpsertField == "Upsert-ID-1" 搜索现有文档并更新这些文档,而不是创建新文档?

环境信息

> mongo --version                                    
MongoDB shell version v4.0.0                         
git version: 3b07af3d4f471ae89e8186d33bbb1d5259597d51
allocator: tcmalloc                                  
modules: none                                        
build environment:                                   
    distmod: 2008plus-ssl                            
    distarch: x86_64                                 
    target_arch: x86_64                              

> mongoimport --version                              
mongoimport version: r4.0.0                          
git version: 3b07af3d4f471ae89e8186d33bbb1d5259597d51
Go version: go1.8.5                                  
   os: windows                                       
   arch: amd64                                       
   compiler: gc                                      
OpenSSL version: OpenSSL 1.0.2o-fips  27 Mar 2018    

我做错了什么?

您有一个与 TSV header 中的引号相关的问题,与此类似:https://jira.mongodb.org/browse/TOOLS-61

当您查看上面的屏幕截图时,您会注意到您的字段名称不是 MyCustomUpsertField 而是 "MyCustomUpsertField" - 包括引号。

所以你想要做的是从你的文件中删除引号(我强烈建议这样做,因为它在 JSON 级别上看起来很时髦而且我觉得这会在某处引起问题)或找到一种在命令行中使用引号的方法,有点像这样:

mongoimport --db upsert-test --collection data --type tsv --headerline --file upsert-data.tsv --upsertFields "MyCustomUpsertField" -vvv

请注意,我没有尝试过上述方法,我猜它不会按预期运行。