Hi Dan,
It’s true that since BSON is a superset of JSON with additional data types, there are cases where JSON cannot reflect the correct datatype. However, MongoDB Extended JSON provides a workaround for this.
By default, mongoexport will output extended JSON, so this shouldn’t be an issue.
I did a quick test using some extended datatypes:
> db.test.find()
{
"_id": ObjectId("5d032fce3741299f7e32aec0"),
"date": ISODate("2019-06-14T05:25:34.096Z"),
"ts": Timestamp(1, 0),
"minkey": [object MinKey],
"maxkey": [object MaxKey],
"long": NumberLong("1"),
"decimal": NumberDecimal("1")
}
then exporting it:
$ mongoexport -d test -c test
2019-06-14T15:25:41.362+1000 connected to: localhost
{"_id":{"$oid":"5d032fce3741299f7e32aec0"},"date":{"$date":"2019-06-14T05:25:34.096Z"},"ts":{"$timestamp":{"t":1,"i":0}},"minkey":{"$minKey":1},"maxkey":{"$maxKey":1},"long":{"$numberLong":"1"},"decimal":{"$numberDecimal":"1"}}
2019-06-14T15:25:41.362+1000 exported 1 record
also re-importing it into another collection:
$ mongoexport -d test -c test | mongoimport -d test -c test2 --drop
The test2 collection contains the correct datatypes:
> db.test2.find()
{
"_id": ObjectId("5d032fce3741299f7e32aec0"),
"date": ISODate("2019-06-14T05:25:34.096Z"),
"ts": Timestamp(1, 0),
"minkey": [object MinKey],
"maxkey": [object MaxKey],
"long": NumberLong("1"),
"decimal": NumberDecimal("1")
}
If you’re using types listed in the Extended JSON page, it should be ok to use mongoexport. I would suggest testing using your particular dataset to confirm this.
I would mention that one possible issue in using mongoexport/mongoimport would be the need to translate the BSON data into extended JSON, and the reverse during import. This would of course be slower than dumping/restoring the actual BSON data, but this may be unavoidable in your case since you need the upsert feature.
To further clarify this, I have opened DOCS-12805. Please feel free to watch the ticket for updates.
Best regards,
Kevin