I have a large MongoDB collection (approx. 30M records) and each item has an array of unique numbers consisting of 8 digits. Most of the time that array has only 1 element (1 number). I want to find out how many records the collection holds with numbers starting with 4, for example, so I query:
{ "numbers.number": /^4.*/i }
However, the query takes too long, last time it took 20 minutes before I interrupted the execution. So I wonder if there's a way to optimize the query. numbers.number
is indexed. I also tried this one:
{ "numbers.number": /^4[0-9]{7}/}
still takes too long. Here's an example of the document:
{
"_id" : ObjectId("some_id"),
"created_at" : ISODate("2022-10-13T09:32:45.000+0000"),
"source" : {
"created_at" : ISODate("2021-10-13T08:54:06.000+0000"),
"some_id" : NumberInt(234),
"another_id" : NumberInt(11)
},
"first_name" : "Test",
"last_name" : "Test",
"date_of_birth" : "1970-01-01",
"status" : "active",
"numbers" : [
{
"created_at" : ISODate("2022-11-13T09:32:45.000+0000"),
"number" : "40000005",
"_id" : ObjectId("some_id")
}
]
}