Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

375
Views
mongoimport fails to load $uuid field from a JSON file

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

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!