rightrealestate.blogg.se

Mongodb compass query distinct values
Mongodb compass query distinct values












  1. Mongodb compass query distinct values how to#
  2. Mongodb compass query distinct values code#

Though, it may be possible to generalize this. Also, it is tightly coupled to the original schema of the document.

mongodb compass query distinct values

PROS: Almost certainly the fastest method.ĬONS: Involves use of the complicated Aggregation API.

Mongodb compass query distinct values how to#

just curious to learn, how to achieve the same with compass tool as I will be using it frequently. I know how to create the query in mongo shell to select distinct fields. "_id" : ObjectId("55fb59f9241fee91ac4cd886"), I installed only MongoDB Compass and connecting to MongoDB Atlas cluster.

Mongodb compass query distinct values code#

OriginalId: ĭb.test.aggregate(/* from first code snippet */) You could do that like this: db.test.aggregate([ It’s open source and the code is available in Github.You want to use aggregation. Var guidDecoded = new Guid(bytes.Slice(0, bytesWritten).ToArray()).ToString() įinally, if you just need it now without writing code use a Blazor WebAssembly app I made just for this. If (Convert.TryFromBase64String("YOUR_BASE64_STRING_HERE", bytes, out int bytesWritten)) Var bytes = new Span(new byte) //arbitrary number Var base64Encoded = Convert.ToBase64String("YOUR_GUID_HERE") Īnd here is a way to decode a base64 string that contains GUID bytes: Solutionįollows a C# solution which is simple enough, but of course requires to at least copy-paste the code to a project and run it: Worth mentioning here that we were working on this problem with Dean Herringer and he was the first to notice the problem.

mongodb compass query distinct values

No matter how many times I attempted to convert the GUID using various online services to its base64 equivalent, I always failed because these services were converting the GUID to base64 as string!Īnd there’s your problem! Encoding a GUID’s bytes is different than encoding GUID’s representative string value. The value you see is actually a base64 encoded string of the GUID value.Īnd everything was cool for years, up until I had to manually debug a situation where I had to connect directly to MongoDB to search for a document based on a GUID I knew. Far from the previous useless sentence though, mind the blue square because this is how MongoDB stores a GUID. The capture above is showing a part of MongoDB compass connected to a beta database with partially real but old data, located locally. Nevertheless, the schema above gave room for some beautiful data like the following image, with some databases and collections ending up having millions of data located in an availability zone probably near you! The schema of the document is not very important other than the fact that the ID should be of type GUID:

  • 3-byte incrementing counter, initialized to a random value.
  • This random value is unique to the machine and process.

    mongodb compass query distinct values

    Mongodb, get distinct values of a field and output the values as single document. 5-byte random value generated once per process. Mongodb find() query : return only unique values (no duplicates) 0.

    mongodb compass query distinct values

  • 4-byte timestamp value, representing the ObjectId’s creation, measured in seconds since the Unix epoch.
  • ObjectId values are 12 bytes in length, consisting of: ObjectIds are small, likely unique, fast to generate, and ordered. That thought alone and the fact that a unique ID through out all databases for each price would be a nice to have, made me decide for a GUID instead of MongoBD’s ObjectId(). The document-oriented database selected was MongoDB, but I always had in mind a future move to Azure Cosmos DB, so avoiding vendor lock was a requirement. Since the data to be stored were fairly simple, I was lead to a design featuring one documentDB per market. In MongoDB, the distinct aggregation command finds the distinct values for a specified field across a single collection. Markets would be a dynamic thing, meaning different ones being added every once and a while, and old ones potentially removed. Some time ago I created a project for storing product prices but for multiple markets around the world. Have you ever tried to query MongoDB with a GUID? Well I did and it can be confusing!














    Mongodb compass query distinct values