Reputation: 73
I have an App written in C#, where the DB is MongoDB. the Id object is C# Guid, so the _id field in the DB looks like:
"_id": {
"$binary": {
"base64": "tZqfm0KNMUi1GGfCkQQosw==",
"subType": "03"
}
}
And the App is using regular Guid ("9b9f9ab5-8d42-4831-b518-67c2910428b3")
Now, I want to add the ability to search by partial id - if I search for "67c2910428b3", I want to find the object with corresponding id.
how to I add a filter \ query to do that ?
I cannot use BsonRegex, since the field in the db is not "converted" - it will never get Guid-id.
the "simple" solution is to add another field in MongoDB with the Guid as string so i can Search on it - but that seems redundant, since the id is already there.
Upvotes: 2
Views: 866
Reputation: 28336
From the bson spec, binary subtype 3 is the old UUID format. One of the problems with the old format was that it permitted the client to specify the ordering of the bytes.
Decoding from the base-64 string, the value of that field is:
0xb59a9f9b428d3148b51867c2910428b3
Note that this UUID you mention, but is using a client ordering that does not conform to RFC-4122 section 4.1.2, which requires MSB ordering for each field.
A UUID is stored by MongoDB as a binary blob. There are no server-side functions to convert or slice the value into smaller chunks.
You can make comparisons of the entire value as described in Comparison/Sort Order
For example, you could find all values that start with 9b9f9ab5
by querying for
{$gte: Guid("9b9f9ab5-0000-0000-0000-000000000000"), $lt: Guid("9b9f9ab6-0000-0000-0000-000000000000")}
Querying a part in the middle, such as all UUID with a time-mid value of 0x8d42 like 00000000-8d42-0000-0000-000000000000, will not be possible with the value stored as binary.
Upvotes: 2