mongodbmongoimport

mongoimport cant recoginze UUID function


Is it possible to import document like this?

{"item_id": UUID("1efaf16f-9c65-6d08-b431-e33e95e34a5e"), name: "yyy"}

I got next error

Failed: error processing document #1: invalid character 'U' looking for beginning of value

Solution

  • mongoimport is finicky. Need to help it out by specifying the specific kind of binary data. Here is an example of your record fixed to use Binary subtype 4 - UUID

    {
      "item_id": {
        "$binary": {
          "base64": "Hvrxb5xlbQi0MeM+lY5KXg==",
          "subType": "04"
        }
      },
      "name": "yyy"
    }
    

    Notice your 128 bits of UUID data are base64 encoded. Good times! :)

    If you're interested, take a look at the BSON types in MongoDB online docs at https://www.mongodb.com/docs/manual/reference/bson-types/. Take note, there are two types of UUID - the old type and the new type. The old type of UUID is subtype 3, whereas the new type is subtype 4.

    If I am not mistaken, UUID subtype 3 (Legacy format) is operating system and/or programming language specific for big-endian, vs. little-endian byte-order concerns. Subtype 4 is consistent across all which is why it's preferred. Can you imagine one system writes a UUID subtype 3, and another system tries to read it, but because the two systems have different architectures the read of the data reads the data in the wrong byte-order? With subtype 4 any system can write the data, and any system can read it without byte-ordering concerns and be guaranteed the value is the same on both systems.

    If you are using Java, be aware, there is an additional control on the connection object when setting up the client object called uuidRepresentation. See additional details at https://mongodb.github.io/mongo-java-driver/5.2/apidocs/mongodb-driver-core/com/mongodb/MongoClientSettings.Builder.html#uuidRepresentation(org.bson.UuidRepresentation)

    As pointed out in the comments by user Joe, you can use the extended JSON representation of your document in your mongoimport data file...

    {
      "item_id": { "$uuid": "1efaf16f-9c65-6d08-b431-e33e95e34a5e" },
      "name": "yyy"
    }
    

    The $uuid operator aligns with the RFC 4122 standard and will be interpreted as subtype 4. If you need subtype 3 (unlikely, but you never know), you must use the base64 approach.