How do I use a guid in a mongodb shell query

爱⌒轻易说出口 提交于 2019-11-28 22:55:55

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?

You can use easily:

.find({ "_id" : CSUUID("E3E45566-AFE4-A564-7876-AEFF6745FF")})
Todd

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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!