How to index dynamic attributes in MongoDB
I also asked this same question (in a bit expanded form) on the mongodb-user mailing list, where I got an answer. Read from there to get more details. The short answer is, that the strategy used in the question should work fine, but there's an issue that makes it very inefficient. Hopefully, the issue will be fixed soon.

For my case, I only need to query for exact matches for the tuple {n,v}, so I can create a multikey index:

db.mycollection.ensureIndex({"attrs":1})


and make they query like this:

db.mycollection.find({"attrs": {n: "subject", v: "Some subject"}})


which works great and uses the index very effectively.