Regex with null character prefix

I have a collection which holds _id (indexed field) that starts with null character and also has some of them in the middle.
I noticed that when I try to $regex a prefix that starts with a null character, the query looks at all the keys in the collection.
Example query (some names changed for simplicity, but the start and end of the regex is the same)
db.collection_example.find({_id: {$regex: '^\\000versions\\000backup1,\\000vaa.*'}).explain("executionStats")

I get: "totalKeysExamined" : 6431140 (this is the total number of documents in the collection)

When I try the same query but omit the first null character I get the following:
db.collection_example.find({_id: {$regex: '^versions\\000backup1,\\000vaa.*'}).explain("executionStats")

I get: "totalKeysExamined" : 0 (of course there are no documents that start with this name, but the query still returns pretty fast and does not try the whole collection)

Do you think my query is wrong for this case, or does mongo has some issue with indexing and searching values that start with a null character?

Thanks for any reply!

I noted that having other null characters in the indexed field makes the issue occur, when replacing with a different character that is not null the issued seem to be gone.
Would still want to know if theirs a workaround or an explanation why this occurs.

I do not think it is a good idea to have null character.

You could have an _id like:

_id : {
  "version" : 1
  "backup" : "vaa"

That would simplify your life as you could avoid using $regex and back-slashes and then having to scan your strings to find the parts that interest you.

I do not think you need the .* at the end of your regex.

1 Like

Thanks for the reply!
I have some limitations in my application that allow me to maybe change this to another character, but I still need the same format.
Do you have some further explanation why the null character is not treated as any other?

I have no clue. Hopefully someone from MongoDB will pick up the thread.