How to import json into MongoDB with Mongo-Express

Jasmine H
2 min readNov 24, 2023

--

Mongo-express is a web-based MongoDB admin interface (GitHub link) which provide some UI buttons for export and import. The version of mongo-express I used in this article is 1.0.0.

Export

There are 2 buttons that can export data.

  • Yellow buttons from “Viewing Collection” page
  • Yellow button from “Viewing Datadabe” page: this is the same as “Export Standard” in “Viewing Collection” page.

After exporting, you can see a file downloaded with the format of data like this:

{"_id":"64d302da57baacdd6d3d12331","name":"test"}
{"_id":"364d304th57gafcdd3d3d1530","name":"test2"}
{"_id":"376ee57a57ba0yj46d02da571","name":"test3"}
{"_id":"1897geibbj7gafcdd3d2gt0od","name":"test4"}

Import

Prepare data to import, allof the following formats can work:

  • Without commas between lines
{"_id":"364d302da57baacdd6d3d123","name":"test1"}
{"_id":"364d302da57baacdd6d3d124","name":"test2"}
  • With commas between lines:
{"_id":"364d302da57baacdd6d3d123","name":"test1"},
{"_id":"364d302da57baacdd6d3d124","name":"test2"}
  • With ISODate field
[
{"_id":"64d2d8e677e2f848cf3ae4d4","import_dt":ISODate("2023-12-17T16:00:00.000Z"),"name":"test1"},
{"_id":"64d2d8e677e2f848cf3ae508","import_dt":ISODate("2023-12-17T16:00:00.000Z"),"name":"test2"},
...
]

[Trouble Shooting] Import Error Cases

If the import format is incorrect, no error message will appear on the webpage. You will need to check the Mongo Express log for details (you’ll see messages such as “Error: operation passed in cannot be an array”)

These are some cases that I’ve tried and could cause import errors:

  1. {“$date”:”2022–11–14T22:58:24.908Z”}

This can cause parser errors. Can be fixed by removing the “date” field, or change the key “date” into “exp_date”.

2. “_id”:ObjectID()
ObjectID() does not work when importing too.

3. Put a [ ] to contains all json
There were a Github discussion which mentioned adding a bracket [ ] can do the trick to import, but when I tested, I have to remove the bracket to make it work.

--

--

Jasmine H

Data Engineer from Taiwan, recently working on EFK and Kubernetes projects.