https://www.mongodb.com/developer/products/mongodb/mongoimport-guide/
docker run --rm -it mongo:5 mongoimport
- uri protocal
mongodb://[user:password@]host:port/[dbname]?authSource=admin
- e.g.
mongoimport --uri 'mongodb://root:[email protected]:27017/db_test?authSource=admin'
the json data should be dict/object like.
mongoimport --collection='mycollectionname' --file=path_json_file
mongoimport --collection='mycollectionname' --file=path_json_array_file --jsonArray
- if your data has
_id
field, then just simply supply the option--mode=upsert
- If you're upserting records that don't have
_id
, you can specify some fields to use to match against documents in the collection, with the--upsertFields
option--upsertFields=name,address,height
- If you are supplied with data files which extend your existing documents by adding new fields, or update certain fields, you can use mongoimport with "merge mode".
--mode=merge
- You can also use the
--upsertFields
option here as well as when you're doing upserts, to match the documents you want to update.
Option | Description |
---|---|
--ignoreBlanks |
Ignore fields or columns with empty values. |
--drop |
Drop the collection before importing the new documents. This is particularly useful during development, but will lose data if you use it accidentally. |
--stopOnError |
Another option that is useful during development, this causes mongoimport to stop immediately when an error occurs. |