31

When using the MongoDB shell, how do I use a guid datatype (which I have used as the _id in my collection).

The following format doesn't work:

>db.person.find({"_id","E3E45566-AFE4-A564-7876-AEFF6745FF"});

Thanks.

Journeyman
  • 10,011
  • 16
  • 81
  • 129

5 Answers5

35

You can use easily:

.find({ "_id" : CSUUID("E3E45566-AFE4-A564-7876-AEFF6745FF")})
Edward Weinert
  • 523
  • 1
  • 5
  • 9
  • 9
    CSUUID is not "standard" in mongo. You need to get the file from the csharpdriver and include it when starting the shell. More information on that here: http://stackoverflow.com/a/8252047/111625 – Sebastien F. Jun 27 '12 at 14:58
19

You could use the following js function in front of your query like so:

function LUUID(uuid) {
    var hex = uuid.replace(/[{}-]/g, ""); // removes extra characters
    return new UUID(hex); //creates new UUID
}

db.person.find({"_id" : LUUID("E3E45566-AFE4-A564-7876-AEFF6745FF"});

You could save the function in .js file and load it or open it before you make your query and if you copy the value from your results you should rename the function with:

  • LUUID for Legacy UUID
  • JUUID for Java encoding
  • NUUID for .net encoding
  • CSUUID for c# encoding
  • PYUUID for python encoding
Pedro del Sol
  • 2,840
  • 9
  • 39
  • 52
Todd
  • 1,071
  • 8
  • 12
  • Thank you. This is by far the best answer in terms of ease of use. None of the BinData and base64 complexity! – Piotr Kula Nov 12 '18 at 13:34
18

You have to compare the _id value against an instance of BinData (not against a string). Unfortunately the BinData constructor takes a Base64 string instead of a hex string.

Your GUID value is missing two hex digits at the end, so for the purposes of this example I will assume they are "00". The following values are equivalent:

hex: "E3E45566-AFE4-A564-7876-AEFF6745FF00" (ignoring dashes)

base64: "ZlXk4+SvZKV4dq7/Z0X/AA=="

So your query should be:

>db.person.find({_id : new BinData(3, "ZlXk4+SvZKV4dq7/Z0X/AA==")})

I am assuming that the binary subtype was correctly set to 3. If not, what driver was used to create the data?

Community
  • 1
  • 1
Robert Stam
  • 12,039
  • 2
  • 39
  • 36
  • 3
    oh dear, that's not very user-friendly is it! I'm going to be hydrating a MongoDB from MSSQL, using existing guids as keys, and running spot queries now and then. I guess I'll just have to figure out an easy way to manually convert hex guids to base64. Thanks for your help! – Journeyman Apr 13 '11 at 17:39
  • I know... I tried to find a Javascript function that would convert a hex string to a Base64 string but there doesn't seem to be one built in to the language. You can search for functions people have written to do this. Or you could write your import program in C# instead of Javascript? – Robert Stam Apr 13 '11 at 17:54
  • 6
    To convert GUID to base64 string: `Convert.ToBase64String(Guid.NewGuid().ToByteArray())` – Chris Fulstow Jun 03 '11 at 01:04
  • Yeah this is a pretty good argument for not using Guid's, wish I had thought about it before using them everywhere. – Chris Nicola Jan 11 '12 at 21:07
  • You can just have your _ids as strings, or any other type you want. Mongo uses OIDs (binary) by default, but this is not required. – UpTheCreek Aug 24 '12 at 07:05
  • I got this "Error: BinData could not decode base64 parameter (shell):1" – Cooper.Wu Mar 03 '14 at 22:19
  • Here's an online guid to base64 converter if you just need to write a one-time query: http://guid-convert.appspot.com/ – Chad Hedgcock Aug 11 '15 at 16:42
  • In case anyone is wondering how the hex can be converted to base64, or even better, obtain a BinData directly from the UUID in mongo shell, it can be done using `CSUUID` (or equivalent functions if you are using something else than C#) mentioned in [this question](https://stackoverflow.com/q/8244110/525036). The resulting hex can then be properly re-encoded in base64 (e.g. on [this site](https://cryptii.com/pipes/hex-to-base64)) if needed. – Didier L Dec 16 '22 at 18:36
1

I know it's an old issue, but without any additional needs you can use this one:

find({_id:UUID('af64ab4f-1098-458a-a0a3-f0f6c93530b7')})
cama
  • 21
  • 4
0

You can fix this issue by using split() and join() workaround:

for instance if I use "E3E45566-AFE4-A564-7876-AEFF6745FF" hex value with - inside UUID() function, it does not return BinData in mongo so please try removing all the - before passing to UUID function.

db.person.find({"_id":UUID("E3E45566-AFE4-A564-7876-AEFF6745FF".split("-").join(''))});

Or by defining a variable to do it in multiple line:

var uuid = UUID("E3E45566-AFE4-A564-7876-AEFF6745FF".split("-").join(''))
db.person.find({"_id":uuid});

or by creating a simple function:

function BUUID(uuid){
    var str = uuid.split("-").join('');
    return new UUID(str);
}
db.person.find({"_id": BUUID("E3E45566-AFE4-A564-7876-AEFF6745FF")}).pretty();
Vipin
  • 847
  • 1
  • 10
  • 21