0
votes

Let's say I have a record

{
    _id: 100,
    foo: {
        bar: 0,
        baz: 1
    }
}

and I want to update it with mongoimport with CSV

_id,foo.kek
100,9000

However,

mongoimport --type csv --file myfile.csv  --headerline  --mode merge

would rewrite the sub-BSON object foo entirely:

{
    _id: 100,
    foo: {
        kek: 9000
    }
}

Is there any way to do the partial update

{
    _id: 100,
    foo: {
        bar: 0,
        baz: 1,
        kek: 9000
    }
}

with mongoimport? Is there any other efficient way to do it (I have millions of records)?

MongoDB server version: 3.0.14

mongoimport version: r3.4.2

Thanks in advance!

1

1 Answers

2
votes

It is not possible with mongoimport.

--mode merge $sets the whole document:

_, err = up.collection.Upsert(selector, bson.M{"$set": document})

https://github.com/mongodb/mongo-tools/blob/cb82c3f9336da19b9cafabf7f57e5e5b0e875338/mongoimport/mongoimport.go#L544

The simplest way would be to patch the code to tailor your needs if you have golang set up. Otherwise you can import the file into a temporary collection and merge it using your language of choice, or write own import utility.