I have a set of documents (call it "logs") that looks something like this:
{ "_id" : ObjectId("52f523892491e4d58e85d70a"), "ds_id" : "534d35d72491de267ca08e96", "eT" : NumberLong(1391784000), "vars" : [{ "n" : "ActPow", "val" : 73.4186401367188, "u" : "kWh", "dt" : "REAL", "cM" : "AVE", "Q" : 99 }, { "n" : "WinSpe", "val" : 3.06327962875366, "u" : "m/s", "dt" : "REAL", "cM" : "AVE", "Q" : 99 }] }
The vars array contains about 150 subdocuments, not just the two shown above. Now I would like to execute a query that extracts val from two vars in the vars array, which I showed above.
Using the aggregation structure, I was able to come up with the following:
db.logs.aggregate( [ { $match : { ds_id: "534d35d72491de267ca08e96", eT: { $lt : 1391784000 }, vars: { $elemMatch: { n: "PowCrvVld", val: 3 }} } }, { $unwind : "$vars" }, { $match : { "vars.n" : { $in : ["WinSpe", "ActPow"] }}, { $project : { "vars.n" : 1, N : 1} } ]);
While this works, I am running against the limit of 16 MB when running large queries. If I have about 150 subdocuments in a vars array, I would also like to avoid $unwind , if possible.
Using a regular request and using $elemMatch , I was able to get ONE of the values:
db.logs.TenMinLog.find({ ds_id : "534d35d72491de267ca08e96", eT : { $lt : 1391784000 }, vars : { $elemMatch : { n : "PowCrvVld", val : 3 } } }, { ds_id : 1, vars : { $elemMatch : { n : "ActPow", cM : "AVE" } });
As for my question, there is a way to use $ elemMatch in an array several times in the <projection> part of the search. If not, is there another way to easily get these two nested documents without using $ unwind? I am also open to other proposals that will be more effective, which I may not suspect. Thanks!