indexing - Find query using $and operator cannot using index in MongoDB -
i have documents structured below, each array element contains "n" , "v" key , value different types of data. (collection name doc)
{ "_id" : 1, "logs" : [ { "n" : "facility", "v" : 26 }, { "n" : "num", "v" : 6 }, { "n" : "ip", "v" : "137.68.151.104" }, { "n" : "protocol", "v" : "55902/udp" }, { "n" : "port", "v" : "53" } ] }, { "_id" : 2, "logs" : [ { "n" : "facility", "v" : 26 }, { "n" : "num", "v" : 6 }, { "n" : "ip", "v" : "137.68.160.51" }, { "n" : "protocol", "v" : "13438/tcp" }, { "n" : "port", "v" : "13438" } ] }, { "_id" : 3, "logs" : [ { "n" : "facility", "v" : 26 }, { "n" : "num", "v" : 6 }, { "n" : "ip", "v" : "137.68.160.51" }, { "n" : "protocol", "v" : "13434/tcp" }, { "n" : "port", "v" : "53" } ] }, { "_id" : 4, "logs" : [ { "n" : "facility", "v" : 26 }, { "n" : "num", "v" : 6 }, { "n" : "ip", "v" : "137.68.160.184" }, { "n" : "protocol", "v" : "61662/udp" }, { "n" : "port", "v" : "53" } ] }, { "_id" : 5, "logs" : [ { "n" : "facility", "v" : 26 }, { "n" : "num", "v" : 6 }, { "n" : "ip", "v" : "137.68.160.51" }, { "n" : "protocol", "v" : "13435/tcp" }, { "n" : "port", "v" : "13435" } ] }, { "_id" : 6, "logs" : [ { "n" : "facility", "v" : 26 }, { "n" : "num", "v" : 6 }, { "n" : "ip", "v" : "137.68.160.51" }, { "n" : "protocol", "v" : "61662/udp" }, { "n" : "port", "v" : "53" } ] } ...
and indexing below.
>db.doc.getindexes() [ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "db_test.doc" }, { "v" : 1, "key" : { "logs.n" : 1, "logs.v" : 1 }, "name" : "logs.n_1_logs.v_1", "ns" : "db_test.doc" } ]
i want query condition. "ip" "137.68.160.51" , "port" "53"
so, query this.
db.doc.find ( {"$and" : [ {"logs" : { "$elemmatch" : { "n" : "ip", "v" : "137.68.160.51" } } }, {"logs" : { "$elemmatch" : { "n" : "port", "v" : "53" } } } ]})
but, query slow. (over 30seconds) ("ip" == "137.68.160.51" document count 10,000,000 . , result document count nothing.)
i think, mongodb cannot using index when second query using $and operator.
if query, b query , c query using $and operator, $and : [ {a}, {b}, {c} ] . b , c query cannot using index. right?
Comments
Post a Comment