Mongoimport csv files with string _id and upsert

后端 未结 5 2037
清歌不尽
清歌不尽 2021-01-13 05:26

I\'m trying to use mongoimport to upsert data with string values in _id. Since the ids look like integers (even though they\'re in quotes), mongoimport treats them as intege

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-13 06:00

    Just encountered this same issue and discovered an alternative. You can force Mongo to use string types for non-string values by converting your CSV to JSON and quoting the field. For example, if your CSV looks like this:

    key value
    123 foo
    abc bar
    

    Then you'll get an integer field for key 123 and a string field for key abc. If you convert that to JSON, making sure that all the keys are quoted, and then use --type json when you import, you'll end up with the desired behavior:

    {
        "123":"foo",
        "abc":"bar"
    }
    

提交回复
热议问题