[SERVER-2070] upsertFields not updating Created: 08/Nov/10  Updated: 12/Jul/16  Resolved: 01/Dec/10

Status: Closed
Project: Core Server
Component/s: Write Ops
Affects Version/s: 1.6.4
Fix Version/s: 1.7.4

Type: Question Priority: Major - P3
Reporter: Simon Namikas Assignee: Mathias Stearn
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Linux ubuntu 2.6.32-25-generic #45-Ubuntu SMP Sat Oct 16 19:52:42 UTC 2010 x86_64 GNU/Linux"


Attachments: File item-file-update-v1.csv     File item-file-v1.csv    
Participants:

 Description   

Hello, and thank-you in advance.
1) I'm able to import a CSV file by using the following:
mongoimport d test01 -c items --type csv --headerline item-file v1.csv

  • this file has 20 fields listed on the csv header, two of which are
    Item Number and Price.
  • the database 'test01' has a collection 'items' with an Index for
    Item Number as the key field (no duplicates).
  • requirement is to update prices wholesale by importing a concise
    file of only two fields Item Number and Price and leaving the
    remaining 18 fields as is.

I then try the following:
mongoimport -d test01 -c items --type csv --upsertFields "Item Number" --headerline item-file-update-v1.csv

  • with the index on Item Number I get the following:
                                      • connected to: 127.0.0.1
                                        430220/443150 97%
                                        23600 7866/second
                                        imported 24323 objects

                                      • -> no prices have been updated in the collection

  • with no index on the Item Number, I get the following:
                                      • connected to: 127.0.0.1
                                        imported 24323 objects

                                      • -> the items from the csv were inserted as new items duplicating the
                                        collection which is expected.

  • I also tried this:
    mongoimport -d test01 -c items -f "Item Number",Price --type csv --upsertFields "Item Number",Price --headerline item-file-update-v1.csv

-> same results of prices not being updated.
I'm not able to update the prices in the collection by using the
second update csv file. I searched for other upsertFields examples
but to no avail. Help on this would be very much appreciated.

also,
2) would there be an RoR gem that leverages mongoimport for csv file uploads?



 Comments   
Comment by Mathias Stearn [ 01/Dec/10 ]

Oh, btw, upsert probably won't do what you want it to. It would replace the object with the matching Item Number with the lines specified (ie remove all fields other than Item Number and Price). You really want to run through all of them and do db.items.update(

{"Item Number": num}

, {$set:{Price: new_price}}). We don't have an automated tool to do that, but it should be easy to do with a few lines of ruby.

Comment by Mathias Stearn [ 01/Dec/10 ]

The issue was that --upsertFields didn't imply --upsert. If you added --upsert to your first update command it would work. I've modified mongoimport to make --upsert implicit if you specify --upsertFields to hopefully prevent other people from getting confused.

Comment by auto [ 01/Dec/10 ]

Author:

{'login': 'RedBeard0531', 'name': 'Mathias Stearn', 'email': 'mathias@10gen.com'}

Message: mongoimport: --upsertFields should imply --upsert SERVER-2070
/mongodb/mongo/commit/53c8e5e3665e5650aaba28f45d5bd2c6fba96e4b

Generated at Thu Feb 08 02:58:52 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.