Problem with MongoDB-aggregate function

Hi all,
I'm trying to get some data from MongoDB database using the function 'aggregate'. But when I insert the pipeline array in the payload, as it sais in the mongodb node information page that I have used [node-red-node-mongodb (0.2.3)], I get the next error: "ReferenceError: cursor is not defined".
I have used this example in order to get the data. I hope you can help me out.


It may sound like a stupid question but ..
Is that all you have in that function ?
Should that be

msg.payload = [ ... <your aggregate query> ...]
return msg

and also have you changed you mongodb node to expect 'aggregate' ?

1 Like

Yes, that is the aggregate query that I put in the msg.payload as Pipeline array. That's what the MongoDB node documentation sais. And also the MongoDB node operation selected is the 'aggregate' function. I don't know why Node-RED returns me that error.


Thanks for answering.

i didnt undestand from your answer how you put the aggregate query in msg.payload using the function node. Your function node doesnt even return any msg at all ... unless thats not a complete screenshot.

  1. Can you share the complete flow ?
  2. Can you put a complete msg Debug node after that function and copy/paste the result from debug window ?
  3. Can you paste a couple of data records from you mongodb collection ?

Sorry, I was using an inject node to set the query on the msg.payload, maybe it has not been understood. Here I send you the flow that I have used.

[{"id":"c7a877f7.e1d698","type":"tab","label":"Aggregate","disabled":false,"info":""},{"id":"afd75fed.e8936","type":"inject","z":"c7a877f7.e1d698","name":"Inject","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":330,"y":400,"wires":[["6eecdeea.201b4"]]},{"id":"6eecdeea.201b4","type":"function","z":"c7a877f7.e1d698","name":"Aggregate Query","func":"msg.payload = [\n    {\n        $match: {}\n    },\n    {\n        $group: {\n            _id: null,\n            avgAge: {\n                $avg: \"$edad\"\n            }\n        }\n    },\n    {\n        $sort: {}\n    }\n]\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":620,"y":400,"wires":[["a6f169ca.ff2758"]]},{"id":"a6f169ca.ff2758","type":"mongodb in","z":"c7a877f7.e1d698","mongodb":"39adf9d0.a8c876","name":"DB","collection":"RealSociedad","operation":"aggregate","x":850,"y":320,"wires":[["633af756.eb0da8"]]},{"id":"633af756.eb0da8","type":"debug","z":"c7a877f7.e1d698","name":"Debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1030,"y":320,"wires":[]},{"id":"30354167.f7ef5e","type":"inject","z":"c7a877f7.e1d698","name":"Aggregate Query","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"$match\":{}},{\"$group\":{\"_id\":null,\"avgAge\":{\"$avg\":\"$edad\"}}},{\"$sort\":{}}]","payloadType":"json","x":510,"y":300,"wires":[["a6f169ca.ff2758"]]},{"id":"9a520ca4.3a0b9","type":"comment","z":"c7a877f7.e1d698","name":"Here I inject the aggregate query directly on the msg.payload.","info":"","x":500,"y":260,"wires":[]},{"id":"7efdae23.43643","type":"comment","z":"c7a877f7.e1d698","name":"Here I use a function to set the query on the msg.payload.","info":"","x":490,"y":460,"wires":[]},{"id":"39adf9d0.a8c876","type":"mongodb","z":"","hostname":"","topology":"direct","connectOptions":"","port":"27017","db":"futbol","name":""}]

The collection that I'm using contains different football players. The keys are in spanish so maybe you don't understand some parameters. The keys are: "name", "last name", "age" and "city". Here I send you an example.

        "_id" : ObjectId("5f7c5c93aa866ad1769ba10c"),
        "nombre" : "Mikel",
        "apellido" : "Oyarzabal",
        "edad" : 23,
        "pueblo" : "Eibar"
        "_id" : ObjectId("5f7c5e9cc480ecb478b42ad2"),
        "nombre" : "Mikel",
        "apellido" : "Merino",
        "edad" : 23,
        "pueblo" : "Pamplona"
        "_id" : ObjectId("5f7c6d79bd7a1ead7894e24e"),
        "nombre" : "Joseba",
        "apellido" : "Zaldua",
        "edad" : 28,
        "pueblo" : "Donostia"

I replicated your db from the records you send me and tested the flow and ..
you are right .. there seems to be an issue with the aggregate in the mongodb node.

Running a simple query from Robo 3T seems to work so its definitely not something wrong with the syntax

After investigating a little bit more .. i believe a bug was introduced in 66-mongodb.js with last month's update

I think line 282 should be result.toArray(function(cursorError, cursorDocs)

maybe the developers can check ?

You are right, I just changed that line and now it works. Thank you very much @UnborN.

my bad -- I checked my local code -- :277 has function(err,cursor) not function(err, result) -- fixing ...

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.