How do I use a guid in a mongodb shell query

后端 未结 3 716
执笔经年
执笔经年 2020-12-09 08:42

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:

>d         


        
3条回答
  •  醉酒成梦
    2020-12-09 09:15

    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

提交回复
热议问题