4

i have Mongodb collection which contains single field , each day i am receiving 31000 documents and in the collection i have almost 6 months data

Here is how my data looks like in database

 {
"_id" : ObjectId("59202aa3f32dfba00d0773c3"),
"Data" : "20-05-2017 18:38:13 SYSTEM_000_00_SAVING ",
"__v" : 0
 }
 {
"_id" : ObjectId("59202aa3f32dfba00d0773c4"),
"Data" : "20-05-2017 18:38:13 SyTime_000_09_00:00 ",
"__v" : 0
}

here is my code for query

 DBObject query = new BasicDBObject();
 Pattern regex = Pattern.compile("20-05-2017"); 
 query.put("Data", regex);

i have created index but its still slow

                   [
                {
                    "v" : 1,
                    "key" : {
                        "_id" : 1
                    },
                    "name" : "_id_",
                    "ns" : "NOB_SRB.fdevices"
                },
                {
                    "v" : 1,
                    "unique" : true,
                    "key" : {
                        "Data" : 1.0
                    },
                    "name" : "Data_1",
                    "ns" : "NOB_SRB.fdevices"
                }
            ]
1

1 Answer 1

1

Add a start of input anchor ^ to the start of the regex:

Pattern regex = Pattern.compile("^20-05-2017");

Because your regex does not have an anchor, the entire field is searched for the date anywhere in it, which requires every character in the field to be compared.

Sign up to request clarification or add additional context in comments.

Comments

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.