# Mongodb3 count problem

**URL:** <https://discourse.nodered.org/t/mongodb3-count-problem/10675>\
**Category:** General\
**Created:** [30 April 2019 13:01 UTC](https://discourse.nodered.org/t/mongodb3-count-problem/10675 "2019-04-30T13:01:10Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![NicolasC](https://avatars.discourse-cdn.com/v4/letter/n/ee59a6/32.png) [@NicolasC](https://discourse.nodered.org/u/NicolasC)\
**Post date:** [30 April 2019 13:01 UTC](https://discourse.nodered.org/t/mongodb3-count-problem/10675/1 "2019-04-30T13:01:10Z")

</div>

Hi,  
I am using node-red-contrib-mongodb3 driver.  
I got "find", "aggregate" requests work, but I can not make count requests work.

In the function preceeding mongodb3 call, I write :  
msg.collection="mycollection";  
msg.payload={};  
msg.operation="count";  
return msg;

As a response, I get :  
payload: object  
empty

instead of the number of elements.

I would appreciate to have any help or hint !

regards,  
Nicolas

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [1 May 2019 11:23 UTC](https://discourse.nodered.org/t/mongodb3-count-problem/10675/2 "2019-05-01T11:23:46Z")

</div>

Read your code.

`msg.payload={};`

result: msg.payload is empty.

---

<div class="post-metadata">

**Author:** ![NicolasC](https://avatars.discourse-cdn.com/v4/letter/n/ee59a6/32.png) [@NicolasC](https://discourse.nodered.org/u/NicolasC)\
**Post date:** [2 May 2019 11:54 UTC](https://discourse.nodered.org/t/mongodb3-count-problem/10675/3 "2019-05-02T11:54:28Z")

</div>

Thanks for your response.

the msg.payload is supposed to contain the request on the collection.  
If I put : msg.payload=[{"test":"12"}]; to filter the documents in the collection, I still get  
the same response :  
payload: object  
empty

---

<div class="post-metadata">

**Author:** ![moebius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/moebius/32/2872_2.png) [@moebius](https://discourse.nodered.org/u/moebius)\
**Post date:** [2 May 2019 12:19 UTC](https://discourse.nodered.org/t/mongodb3-count-problem/10675/4 "2019-05-02T12:19:25Z")

</div>

- From the doc:

> _Count_ returns a count of the number of documents in a collection or matching a query using the `msg.payload` as the query statement.

---

<div class="post-metadata">

**Author:** ![NicolasC](https://avatars.discourse-cdn.com/v4/letter/n/ee59a6/32.png) [@NicolasC](https://discourse.nodered.org/u/NicolasC)\
**Post date:** [2 May 2019 12:26 UTC](https://discourse.nodered.org/t/mongodb3-count-problem/10675/5 "2019-05-02T12:26:33Z")

</div>

Yes, That's why I Don't understand the behavior.  
I think the problem has to do with the mongodb3 node.

I just tried withe mongoDB2, and the results are ok.  
Furthermore, I was surprised that find.toArray and aggregate.toArray return an object in mongodb3, vs an array, in MongoDB and mongodb2.

Maybe It is worth using Mongodb2 driver ...

---

<div class="post-metadata">

**Author:** ![moebius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/moebius/32/2872_2.png) [@moebius](https://discourse.nodered.org/u/moebius)\
**Post date:** [2 May 2019 12:54 UTC](https://discourse.nodered.org/t/mongodb3-count-problem/10675/6 "2019-05-02T12:54:32Z")

</div>

Count is working here...  
mongodb3 version 1.0.0

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/4/4e11350d0035ebe4255c4cbb0f733ba694def724.png)

---

<div class="post-metadata">

**Author:** ![NicolasC](https://avatars.discourse-cdn.com/v4/letter/n/ee59a6/32.png) [@NicolasC](https://discourse.nodered.org/u/NicolasC)\
**Post date:** [2 May 2019 13:23 UTC](https://discourse.nodered.org/t/mongodb3-count-problem/10675/7 "2019-05-02T13:23:17Z")

</div>

interesting, … I have 2.0.0 (and mongo db version is 3.6.12)

---

<div class="post-metadata">

**Author:** ![moebius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/moebius/32/2872_2.png) [@moebius](https://discourse.nodered.org/u/moebius)\
**Post date:** [2 May 2019 13:38 UTC](https://discourse.nodered.org/t/mongodb3-count-problem/10675/8 "2019-05-02T13:38:59Z")

</div>

Maybe you've found a bug...  
Server here is 3.6.4, but i would start looking at the node.

---

<div class="post-metadata">

**Author:** ![NicolasC](https://avatars.discourse-cdn.com/v4/letter/n/ee59a6/32.png) [@NicolasC](https://discourse.nodered.org/u/NicolasC)\
**Post date:** [6 May 2019 15:35 UTC](https://discourse.nodered.org/t/mongodb3-count-problem/10675/9 "2019-05-06T15:35:51Z")

</div>

I juste tested with mondodb3 driver.  
1.0.0 works,  
2.0.0 (and 2.0.1) don't work… (I upgraded from 1.0.0 to 2.0.0)

Thanks for your help

---

<div class="post-metadata">

**Author:** ![ronopitz](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ronopitz/32/14486_2.png) [@ronopitz](https://discourse.nodered.org/u/ronopitz)\
**Post date:** [21 November 2019 10:40 UTC](https://discourse.nodered.org/t/mongodb3-count-problem/10675/10 "2019-11-21T10:40:29Z")

</div>

The Problem still exists.

MongoDB 3.6.14  
mongodb3 Node 2.0.1

Does anyone know a solution or workaround?

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![adanner](https://avatars.discourse-cdn.com/v4/letter/a/dec6dc/32.png) [@adanner](https://discourse.nodered.org/u/adanner)\
**Post date:** [30 June 2020 09:28 UTC](https://discourse.nodered.org/t/mongodb3-count-problem/10675/11 "2020-06-30T09:28:58Z")

</div>

Hi Nicolas,

I came along your problem description as I got stuck with the use of find and aggregate in mongodb2 and mongodb3 nodes. You wrote that you managed to get find an aggregate to work, which is exactly the problem that I have - both are not working - neither in mongodb2 nor in mongodb3.

In both case I use the dynamic configuration of the node as the drop down list has neither the find entry nor one for the aggregate command. So I set the msg.operation to "find" or "aggregate" vice versa.

In both cases I got the following error: "no operation defined"

regards Andre

---

<div class="post-metadata">

**Author:** ![NicolasC](https://avatars.discourse-cdn.com/v4/letter/n/ee59a6/32.png) [@NicolasC](https://discourse.nodered.org/u/NicolasC)\
**Post date:** [30 June 2020 12:15 UTC](https://discourse.nodered.org/t/mongodb3-count-problem/10675/12 "2020-06-30T12:15:09Z")

</div>

Hi,

msg.operation="find.toArray";

or “aggregate.toArray” should work !

---

<div class="post-metadata">

**Author:** ![adanner](https://avatars.discourse-cdn.com/v4/letter/a/dec6dc/32.png) [@adanner](https://discourse.nodered.org/u/adanner)\
**Post date:** [3 July 2020 06:59 UTC](https://discourse.nodered.org/t/mongodb3-count-problem/10675/13 "2020-07-03T06:59:38Z")

</div>

thanks, indeed this was the missing piece 🙂 What I do not really understand why the mongodb2 node is offering a dynamic configuration, when I cannot use the basic find() or is it limited to the functions that are offered in the drop down box ?

BR Andre
