GUID Query Through Mongo Shell

First published: 2015-02-23      |      Last updated: 2026-02-05

Introduction

If you have stored a GUID through the C## driver to mongoDB and now you want to run a query by GUID, you can't query directly because mongoDB doesn't recognize GUID so when we query through mongo shell no result will be returned. To use the power of mongo shell for querying data on mongo by GUID, you should follow these steps.

1. Convert GUID data to Base64

Convert you GUID data to base64 , you can use any online tool for this.

So suppose your GUID is: 00112233-4455-6677-8899-aabbccddeeff

Then the base 64 version will be: MyIRAFVEd2aImaq7zN3u/w==

2. Query by BinData object in mongo shell

1db.Users.find({"useUniqueId": new BinData(3,"MyIRAFVEd2aImaq7zN3u/w==")}).limit(1)

Actually BinData constructor takes 2 parameters:

New BinData(subtype,data)

  • Subtype: represent subdata type like we pass 3 for UUID or GUID
  • Data: base64 encoded string data

mongoDB's C## driver stores data to mongo by converting it into binary data rather than string.

Kundan Singh
By Kundan SinghKundan Singh serves as the Vice President of Engineering and Information Security at LoginRadius. With over 15 years of hands-on experience in the Customer Identity and Access Management (CIAM) landscape, Kundan leads the strategic direction of our security architecture and product reliability.

Prior to LoginRadius, Kundan honed his expertise in executive leadership roles at global giants including BestBuy, Accenture, Ness Technologies, and Logica. He holds an engineering degree from the Indian Institute of Technology (IIT), blending a rigorous academic foundation with deep enterprise-level security experience.
cardImage

The State of Consumer Digital ID 2024

cardImage

Top CIAM Platform 2024

cardImage

Learn How to Master Digital Trust

Customer Identity, Simplified.

No Complexity. No Limits.
Thousands of businesses trust LoginRadius for reliable customer identity. Easy to integrate, effortless to scale.

See how simple identity management can be. Start today!