I want to use mongoimport to load a JSON file that includes UUIDs fields.
A program that creates JSON files serialise UUIDS like this:
{ "_id": {"$uuid": "9ab62b5e-f34a-3854-b8bf-df7ee0102229"}, "foo": "bar" }
In the collection to which I want to import the document UUID is represented in the binary format:
{ "_id" : BinData(3,"eT/WZXnp96m5uHOpApFSmg=="), "foo" : "bar"}
mongoimport doesn't like the $uuid bit, and fails with
$ cat foo.json| mongoimport -h 127.0.0.1 -d mydb --collection mycollection
2017-01-30T23:46:05.060+0100 connected to: 127.0.0.1
2017-01-30T23:46:05.079+0100 error inserting documents: $uuid is not valid for storage.
2017-01-30T23:46:05.079+0100 imported 0 documents
Any ideas how could I get around this problem
According to mongoimport documentation
Warning
Avoid using mongoimport and mongoexport for full instance production backups. They do not reliably preserve all rich BSON data types, because JSON can only represent a subset of the types supported by BSON. Use mongodump and mongorestore as described in MongoDB Backup Methods for this kind of functionality.
So understandably that UUID turned binary data doesn't come under valid JSON data types hence it fails, you can try mongodump it's more robust.
I went out and decided to write a script that converts $uuid to $binary that seems to be accepted by mongoimport and the records are properly inserted into mongodb.
this is how I use it:
cat exported.json | uuid_to_bindata.rb | mongoimport -h 127.0.0.1 -d mydb --collection mycollection
This is the uuid_to_bindata.rb script.