# Problem with MongoDB-aggregate function

**URL:** <https://discourse.nodered.org/t/problem-with-mongodb-aggregate-function/35601>\
**Category:** General\
**Created:** [9 November 2020 14:03 UTC](https://discourse.nodered.org/t/problem-with-mongodb-aggregate-function/35601 "2020-11-09T14:03:48Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonlega06](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jonlega06/32/31660_2.png) [@jonlega06](https://discourse.nodered.org/u/jonlega06)\
**Post date:** [9 November 2020 14:03 UTC](https://discourse.nodered.org/t/problem-with-mongodb-aggregate-function/35601/1 "2020-11-09T14:03:48Z")

</div>

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.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/8/f823b57b45435fca0cd1c560279f51811c859953.png)

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [10 November 2020 13:59 UTC](https://discourse.nodered.org/t/problem-with-mongodb-aggregate-function/35601/2 "2020-11-10T13:59:51Z")

</div>

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

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

```

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

---

<div class="post-metadata">

**Author:** ![jonlega06](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jonlega06/32/31660_2.png) [@jonlega06](https://discourse.nodered.org/u/jonlega06)\
**Post date:** [10 November 2020 14:11 UTC](https://discourse.nodered.org/t/problem-with-mongodb-aggregate-function/35601/3 "2020-11-10T14:11:19Z")

</div>

Yes, that is the aggregate query that I put in the msg.payload as Pipeline array. That's what the [MongoDB node documentation](https://flows.nodered.org/node/node-red-node-mongodb) sais. And also the MongoDB node operation selected is the 'aggregate' function. I don't know why Node-RED returns me that error.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/5/252af6440ac0b2b9fca27df891f8ef5bb342998a.png)

Thanks for answering.

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [10 November 2020 14:39 UTC](https://discourse.nodered.org/t/problem-with-mongodb-aggregate-function/35601/4 "2020-11-10T14:39:34Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![jonlega06](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jonlega06/32/31660_2.png) [@jonlega06](https://discourse.nodered.org/u/jonlega06)\
**Post date:** [10 November 2020 15:07 UTC](https://discourse.nodered.org/t/problem-with-mongodb-aggregate-function/35601/5 "2020-11-10T15:07:16Z")

</div>

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.

```auto
[{"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":"127.0.0.1","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.

```auto
{
        "_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"
}

```

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [10 November 2020 17:18 UTC](https://discourse.nodered.org/t/problem-with-mongodb-aggregate-function/35601/6 "2020-11-10T17:18:19Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/e/7e90aec7337899dc2e7f52e273c2609146024403.png)

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [10 November 2020 22:13 UTC](https://discourse.nodered.org/t/problem-with-mongodb-aggregate-function/35601/7 "2020-11-10T22:13:04Z")

</div>

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

> <https://github.com/node-red/node-red-nodes/commit/4ae0818a4b9248554c000bc4f33d47cd10bf9cb0#diff-c8e7770ed9979e6c1547ac0cdaf2492534099e15952f150b7bcd29932f9929b9>

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

maybe the developers can check ?

---

<div class="post-metadata">

**Author:** ![jonlega06](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jonlega06/32/31660_2.png) [@jonlega06](https://discourse.nodered.org/u/jonlega06)\
**Post date:** [11 November 2020 10:04 UTC](https://discourse.nodered.org/t/problem-with-mongodb-aggregate-function/35601/8 "2020-11-11T10:04:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ibmrcruicks](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ibmrcruicks/32/83099_2.png) [@ibmrcruicks](https://discourse.nodered.org/u/ibmrcruicks)\
**Post date:** [11 November 2020 10:34 UTC](https://discourse.nodered.org/t/problem-with-mongodb-aggregate-function/35601/9 "2020-11-11T10:34:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [8 December 2020 11:24 UTC](https://discourse.nodered.org/t/problem-with-mongodb-aggregate-function/35601/11 "2020-12-08T11:24:43Z")

</div>

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