mongodb indexing subarray values -
if have in mongodb array structure this:
"field": [ [10,20] //specified length per array [25,40] [60,90] ...and on, various size per document ] how can index subarrays? tried index with: db.ensureindex({"field.$.0":1}); //and {"field.$.1":1}
but searching in documents remained slow.
i tried solution: mongodb, array of arrays index but
db.ensureindex("{field:[{"a":1}]}"); //and "{field:[{"b":1}]}" if naming subarray indexes , b, sintax throws "bad index key pattern" exception.
can explain more you're trying do? first schema design not good; have bunch of arrays have no way address except using array operators can slow.
it seems on track second idea syntax little off. if can insert documents instead of sub-arrays you'll find schema easier deal , can index on 2 values in each document illustrated below:
> db.test.insert({field: [{a:1, b:2}, {a:3, b:4}]}) > db.test.ensureindex({"field.a":1}) > db.test.ensureindex({"field.b":1}) > db.test.getindexes() [ { "v" : 1, "key" : { "_id" : 1 }, "ns" : "test.test", "name" : "_id_" }, { "v" : 1, "key" : { "field.a" : 1 }, "ns" : "test.test", "name" : "field.a_1" }, { "v" : 1, "key" : { "field.b" : 1 }, "ns" : "test.test", "name" : "field.b_1" } ] > db.test.find({"field.a": 3}) { "_id" : objectid("520e2ec749177daf439a2ff6"), "field" : [ { "a" : 1, "b" : 2 }, { "a" : 3, "b" : 4 } ] } you can run explain see that, indeed, index being used (see cursor line)
> db.test.find({"field.a": 3}).explain() { "cursor" : "btreecursor field.a_1", "ismultikey" : true, "n" : 1, "nscannedobjects" : 1, "nscanned" : 1, "nscannedobjectsallplans" : 1, "nscannedallplans" : 1, "scanandorder" : false, "indexonly" : false, "nyields" : 0, "nchunkskips" : 0, "millis" : 0, "indexbounds" : { "field.a" : [ [ 3, 3 ] ] }, "server" : "xxxxx-pc:27017" }
Comments
Post a Comment