Request regular request MongoDB

I have a Mongodb collection that contains one field, every day I get 31,000 documents, and in the collection I have almost 6-month data.

This is how my data looks in the 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 the request

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

I created an 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"
                }
            ]
+4
source share
1 answer

Add the beginning of the input anchor ^to the beginning of the regular expression:

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

Since your regular expression is not bound, the entire field looks for a date anywhere, which requires a comparison of each character in the field.

+1
source

Source: https://habr.com/ru/post/1685789/


All Articles