F.P
F.P

Reputation: 17831

mongimport upsert creates new documents

When I try to execute a mongoimport with upsertFields like so:

> 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

And then execute the same command again, The result is two documents with exactly the same data.

Adding the (supposedly obsolete) --mode upsert flag changes nothing. New documents are always created.

Dubplicate documents with same Data despite upsertFields

I was under the impression, that upserFields would search for already existing documents with MyCustomUpsertField == "Upsert-ID-1" and update those documents instead of creating new ones?

Env Info

> 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    

What am I doing wrong?

Upvotes: 0

Views: 50

Answers (1)

dnickless
dnickless

Reputation: 10918

You have an issue related to quotes in your TSV header which is similar to this one: https://jira.mongodb.org/browse/TOOLS-61

When you look at your screenshot above you'll notice that your field names are not MyCustomUpsertField but "MyCustomUpsertField" - with the quotes included.

So what you want to do is either remove the quotes from your file (I would strongly suggest that because it looks funky on a JSON level and I feel this is going to cause issues somewhere) or find a way to use quotes in the command line, kind of like this:

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

Mind you, I haven't tried the above and would guess it won't behave as expected.

Upvotes: 1

Related Questions