Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8397695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:51:38+00:00 2026-06-09T20:51:38+00:00

I’m adding a ‘GUID’ key with a value of uuid.uuid1() (from python uuid module)

  • 0

I’m adding a ‘GUID’ key with a value of uuid.uuid1() (from python uuid module) for all my documents in Mongo. I noticed they are being stored not as strings, but as type BSON::Binary. I’ve done some Googling already, but I still don’t understand what the purpose/advantage to this serialization is. Can someone explain? Should I be converting the uuid.uuid1() to strings before storing? How can I use a string to find() by the GUID value like db.myCol.find({ ‘GUID’ : aString })?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:51:40+00:00Added an answer on June 9, 2026 at 8:51 pm

    The default serialization for a Python uuid uses a UUID binary representation in the BSON spec because this ensures consistent sorting for range queries, and also uses less storage for data/indexes.

    For example, these three strings are equivalent in hex:

    5d78ad35ea5f11e1a183705681b29c47
    5D78AD35EA5F11E1A183705681B29C47
    5d78ad35ea5f11e1A183705681B29C47
    

    ..but have different sort orders as strings:

    > db.uuidsort.find().sort({_id:1})
    { "_id" : "5D78AD35EA5F11E1A183705681B29C47" }
    { "_id" : "5d78ad35ea5f11e1A183705681B29C47" }
    { "_id" : "5d78ad35ea5f11e1a183705681b29c47" }
    

    Comparing the bson sizes:

    > db.uuidtest.find()
    { "_id" : BinData(3,"XXitNepfEeGhg3BWgbKcRw==") }
    { "_id" : "5d78ad35ea5f11e1a183705681b29c47" }
    
    > Object.bsonsize(db.uuidtest.findOne({_id: BinData(3,"XXitNepfEeGhg3BWgbKcRw==")}))
    31
    
    > Object.bsonsize(db.uuidtest.findOne({_id: "5d78ad35ea5f11e1a183705681b29c47"}))
    47
    

    If you do want to insert as strings, you can use UUID.hex to get the 32-character string equivalent:

    >>> db.uuidtest.insert({'_id': uuid.hex})
    '5d78ad35ea5f11e1a183705681b29c47'
    

    If you want to find UUIDs by string from Python, you can use the uuid.UUID methods:

    >>> db.uuidtest.find_one({'_id':uuid.UUID('5d78ad35ea5f11e1a183705681b29c47')})
    {u'_id': UUID('5d78ad35-ea5f-11e1-a183-705681b29c47')}
    

    If you want to find UUIDs by string from the mongo shell, there is a UUID() helper:

    > db.uuidtest.find({_id:UUID('5d78ad35ea5f11e1a183705681b29c47')})
    { "_id" : BinData(3,"XXitNepfEeGhg3BWgbKcRw==") }
    

    Note: there are a few other UUID subtypes available for interoperability with other driver versions, as described in the API docs for bson.binary.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from
I have a view passing on information from a database: def serve_article(request, id): served_article
I have a reasonable size flat file database of text documents mostly saved in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.