Can you explain more what you're trying to do? The first schema design is not very good; you have a bunch of arrays that you really have no way to address except for using the array operators which can be very slow.
It seems like you were on track with your second idea however your syntax is just a little off. If you can insert documents instead of sub-arrays you'll find the schema much easier to deal with and you can index on the two values in each document as 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 an explain to see that, indeed, the index is being used (see the 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"
}