3

My application currently performs a regex search on a text field which is a comma separated Objectids. According to Mongodb documentation, Mongo uses indexes while doing regex searches.

My initial thought was to use an array to store the ObjectIds instead of using the string. But will the array search have better performance than the regex search since both are using indexes?

1 Answer 1

3

Using an array of ObjectIds instead of a comma-separated list of ObjectId strings is the way to go here.

  1. An array will use less space: an ObjectId string is 24 characters while a BSON ObjectId is 12 bytes.
  2. An array index is more effective: for a regex search that isn't rooted to the beginning of the text (i.e. not starting with ^), the entire index must be searched O(n) while with an array, each element has its own multikey index entry O(log n).
  3. The size of an index entry must be less than 1024 bytes, which would limit you to about 42 ObjectIds in a text field.
  4. Array elements are atomically modifiable: you can use array update operators to directly modify individual elements.
Sign up to request clarification or add additional context in comments.

2 Comments

"while with an array, each element has its own index entry". Can you give a little more insight into this?
@Rahul I added a link to the multikey index docs that are used with array fields.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.