# Problem use aggregate with mongodb3

**URL:** <https://discourse.nodered.org/t/problem-use-aggregate-with-mongodb3/64191>\
**Category:** General\
**Created:** [21 June 2022 03:01 UTC](https://discourse.nodered.org/t/problem-use-aggregate-with-mongodb3/64191 "2022-06-21T03:01:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![junoprima](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/junoprima/32/63547_2.png) [@junoprima](https://discourse.nodered.org/u/junoprima)\
**Post date:** [21 June 2022 03:01 UTC](https://discourse.nodered.org/t/problem-use-aggregate-with-mongodb3/64191/1 "2022-06-21T03:01:47Z")

</div>

Hi I try to use aggregate in mongodb3 node but result not true

my flow

```auto
[
    {
        "id": "b583c2b096c05a21",
        "type": "mongodb3 in",
        "z": "d851478c2fe7e179",
        "service": "_ext_",
        "configNode": "f0ce3a84bbff14f4",
        "name": "",
        "collection": "",
        "operation": "aggregate.toArray",
        "x": 610,
        "y": 400,
        "wires": [
            [
                "9f3fcdcb4a825e8d"
            ]
        ]
    },
    {
        "id": "c759e5a57184164f",
        "type": "function",
        "z": "d851478c2fe7e179",
        "name": "",
        "func": "const date1 = new Date('2022-06-20 16:06:00');\nconst date2 = new Date('2022-06-20 17:05:00')\n\nvar newMsg = {};\nnewMsg.collection = \"markup_5min\";\nnewMsg.payload =[\n { \n $match:{\n datetime : { \n $gte:date1, \n $lte:date2\n \n },\n imei:'866416049315865'\n }\n },{\n $group:{\n _id: \"null\",\n pulse_for: {$sum:\"$pulse\"},\n count: {$sum:1}\n }\n }\n]\n\nreturn newMsg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 340,
        "y": 400,
        "wires": [
            [
                "b583c2b096c05a21"
            ]
        ]
    },
    {
        "id": "9f3fcdcb4a825e8d",
        "type": "debug",
        "z": "d851478c2fe7e179",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 730,
        "y": 480,
        "wires": []
    },
    {
        "id": "7ac10671ac811fb5",
        "type": "inject",
        "z": "d851478c2fe7e179",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 200,
        "y": 400,
        "wires": [
            [
                "c759e5a57184164f"
            ]
        ]
    },
    {
        "id": "f0ce3a84bbff14f4",
        "type": "mongodb3",
        "uri": "mongodb://192.168.1.21,192.168.1.22,192.168.1.23/Master_Data?replicaSet=rs0",
        "name": "Master_Data",
        "options": "",
        "parallelism": "-1"
    }
]

```

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

But result in Navicat I use is true

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/1/b1c800e86847b3dde4903ad976c5a731751d0549.png)

what the problem with my bordered code

Thank

---

<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:** [21 June 2022 11:08 UTC](https://discourse.nodered.org/t/problem-use-aggregate-with-mongodb3/64191/2 "2022-06-21T11:08:38Z")

</div>

you are right .. there is something wrong with this node's aggregate option

i replaced the node with **[node-red-node-mongodb](https://flows.nodered.org/node/node-red-node-mongodb)** and with that the command works fine

---

<div class="post-metadata">

**Author:** ![chrisn-au](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chrisn-au/32/25504_2.png) [@chrisn-au](https://discourse.nodered.org/u/chrisn-au)\
**Post date:** [26 June 2022 20:59 UTC](https://discourse.nodered.org/t/problem-use-aggregate-with-mongodb3/64191/3 "2022-06-26T20:59:11Z")

</div>

looking at the query I feel the problem maybe with the query. The queryneeds to be an array of parts where the first part is the aggregation. Try wrapping the query array in an array like

```auto
newMsg.payload =[
        { 
            $match:{
                datetime : { 
                    $gte:date1, 
                    $lte:date2
                    
                },
                imei:'866416049315865'
            }
        },{
            $group:{
                _id: "null",
                pulse_for: {$sum:"$pulse"},
                count: {$sum:1}
            }
        }
    ]

```

should be

```auto
newMsg.payload = [
[
        { 
            $match:{
                datetime : { 
                    $gte:date1, 
                    $lte:date2
                    
                },
                imei:'866416049315865'
            }
        },{
            $group:{
                _id: "null",
                pulse_for: {$sum:"$pulse"},
                count: {$sum:1}
            }
        }
    ]
]

```

---

<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:** [25 August 2022 20:59 UTC](https://discourse.nodered.org/t/problem-use-aggregate-with-mongodb3/64191/4 "2022-08-25T20:59:43Z")

</div>

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