0
votes

I have a use case where storing an _id that is a composite of the following would be immensely useful:

  1. 96 bit int
  2. 96 bit int
  3. 96 bit int ObjectID

This works out as a 36 byte ASCII string. It will be byte reversed to become big endian.

I would like to query "Give me all documents where _id starts with {1} concatenate {2}." This would return documents where _id matches components 1 & 2 above, with any 3.

Yes, this is a Cassandra approach. The rest of the query I have can only be done realistically in MongoDB.

How can I do this?

For reference, I am using C#.

1

1 Answers

1
votes

With your IDs saved as a 36-byte ascii strings, you can use a regular expression match to find prefix substrings in the index.

The caveats for efficiently using an index with regular expressions in MongoDB are that:

  • the regex match should be left-rooted (which will be the case for your "_id starts with" search)
  • indexes are case-sensitive

The query would be similar to:

/* Assumption: comp1 and comp2 are ASCII string representations
   that can be combined for a key prefix */

var spec = new Document("_id", new MongoRegex(string.Format("^{0}{1}", comp1, comp2)));
collection.Find(spec)