Let's say I have the following MongoDB document.
{
(...)
"services": {
"TCP80": {
"data": [{
"status": 1,
"delay": 3.87,
"ts": 1308056460
},{
"status": 1,
"delay": 2.83,
"ts": 1308058080
},{
"status": 1,
"delay": 5.77,
"ts": 1308060720
}]
}
}}
And I wish to retrieve all the entries in the "data" array which whose "ts" value is greater than "1308056460". Therefore, the expected result would be,
[
{
"status": 1,
"delay": 2.83,
"ts": 1308058080
},{
"status": 1,
"delay": 5.77,
"ts": 1308060720
}
]
One way to do this would to use the MongoDB aggregate function.
db.test.aggregate(
{$match : {}},
{$unwind: "$services.TCP80.data"},
{$match: {"services.TCP80.data.ts": {$gte: 1308060720}}}
);
The other would be to retrieve the entire data array to by middle layer (a node.js application) and run a loop to filter out the values that I want.
Leaving aside implementation simplicity, which approach would be more efficient or yield faster results ? and Why ?