1

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 ?

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
Heshan Perera
  • 4,592
  • 8
  • 44
  • 57
  • 2
    This is actually fairly subjective, and the general hint here is "Which is best" not really a good premise for a question. Reality tends to point to the fact that your "database" platform "should" have more processing power than your "application" platform. Not to mention that you are transferring unnecessary data between the two otherwise. Just trying to decide if there is a real question with a real answer here. Because if you didn't "expect" the database to do, then why use a database at all? – Neil Lunn Sep 25 '14 at 11:03

1 Answers1

5

In general the MongoDB Aggregate function is expected to be faster, since it is implemented in C, and highly optimized, along with being continuously developed to be faster still.

Having said that,even if that is not the case, as Neil mentioned, good software engineering practices should take priority unless you're building something mission-critical. The database is expected to do all the heavy lifting, and unnecessary transfer of data should be avoided.Code complexity and abstraction should also figure in the consideration.In this case, since MongoDb does provide a query to replicate your logic on the database itself, you should do it on the database server itself.

Abhishek Pathak
  • 1,569
  • 1
  • 10
  • 19