1

I am trying to return an array of results using Mongoose and mongoDB in Node.js.

I have something like this to match every header that starts with za for example:

Model.aggregate(
        { $project: { firstLetter : { $substr : ["$header", 0, 2] }}},
        { $match: { firstLetter : 'za' }},
        { $limit: 40 }
    );

But when I assign the result to a variable it is just an Aggregate object that I cannot identify what to do with.

Mongoose docs state:

The documents returned are plain javascript objects, not mongoose documents (since any shape of document can be returned).

Why am I not getting the results?

Community
  • 1
  • 1
Adam Thompson
  • 3,278
  • 3
  • 22
  • 37

1 Answers1

1

Turns out that you need to get the data asynchronously, which makes sense (of course).

Something like:

Model.aggregate(
        { $project: { firstLetter : { $substr : ["$header", 0, 2] }}},
        { $match: { firstLetter : 'za' }},
        { $limit: 40 }
    ).exec(function(err, data) {
       doSomethingWithData(data);
});

Hopefully can help someone else out.

Adam Thompson
  • 3,278
  • 3
  • 22
  • 37