Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongo and Node.js find and aggregate

I'm using Node.js and MongoDB and I need to use the functions "find" and "aggregate" togheter. For example:

var mongo = require('mongodb')
mongo.MongoClient.connect(uri, {
  useNewUrlParser: true }, function(errorConectDB, client) {
     dbo.collection(this._objectName).find(query,options).aggregate(agr).toArray(function(err,
         res) {....}) })

However, when I execute my code the following error appears:

TypeError: dbo.collection(...).find(...).aggregate is not a function

Is there any way to fix it? Do you recommend any other alternative?

like image 222
Lucas Segura Avatar asked Sep 15 '25 03:09

Lucas Segura


1 Answers

Use $match in aggregate instead of using find as explained here: https://docs.mongodb.com/manual/reference/operator/aggregation/match/

For example:

db.articles.aggregate(
    [ { $match : { author : "dave" } } ]
);
like image 90
GuillaumeHaben Avatar answered Sep 17 '25 19:09

GuillaumeHaben