# Retrieving certain data from a MongoDB

**URL:** <https://discourse.nodered.org/t/retrieving-certain-data-from-a-mongodb/83741>\
**Category:** Dashboard\
**Tags:** database\
**Created:** [15 December 2023 11:18 UTC](https://discourse.nodered.org/t/retrieving-certain-data-from-a-mongodb/83741 "2023-12-15T11:18:32Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Schlitz](https://avatars.discourse-cdn.com/v4/letter/s/3bc359/32.png) [@Schlitz](https://discourse.nodered.org/u/Schlitz)\
**Post date:** [15 December 2023 11:18 UTC](https://discourse.nodered.org/t/retrieving-certain-data-from-a-mongodb/83741/1 "2023-12-15T11:18:33Z")

</div>

Hello everyone,  
I am currently running a project in which I am uploading shipment data (just numbers) to a MongoDB with the help of Node Red. To do this, I have built certain input fields into the NodeRed dashboard. The shipment number can be entered there together with other shipment data. The data is then bundled in a JSON file using a JOIN node and transferred to MongoDB. Now the task: I want to access data that belongs to a specific shippment number. To do this, I have added a text input field in the Node Red dashboard where the shipment number to be searched for in the Mongo DB can be entered. The associated data should be displayed on the dashboard in a text output field. I already have set up a connection to mongo db but when i type in a shipment number, all data is displayed on the dashboard.

Can somebody help? I am a beginner in the field of node red 😃  
Thanks in advance!

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [15 December 2023 18:08 UTC](https://discourse.nodered.org/t/retrieving-certain-data-from-a-mongodb/83741/2 "2023-12-15T18:08:27Z")

</div>

Welcome to the Forums @Schlitz

I have moved this to the dashboard category - To attract the right answers.  
I don't use DB myself - but hopefully someone who does, can consult.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [15 December 2023 18:43 UTC](https://discourse.nodered.org/t/retrieving-certain-data-from-a-mongodb/83741/3 "2023-12-15T18:43:30Z")

</div>

> [@Schlitz](#):
>
> when i type in a shipment number, all data is displayed on the dashboard

What do you mean by that?

---

<div class="post-metadata">

**Author:** ![Schlitz](https://avatars.discourse-cdn.com/v4/letter/s/3bc359/32.png) [@Schlitz](https://discourse.nodered.org/u/Schlitz)\
**Post date:** [15 December 2023 19:18 UTC](https://discourse.nodered.org/t/retrieving-certain-data-from-a-mongodb/83741/4 "2023-12-15T19:18:14Z")

</div>

![bsp](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/c/3cd72e3cc8aa4afad996a52262f85af0342e1f6b.png)

Hi Colin, here is an excerpt of the data from my MongoDB. In the previous step, various data is bundled together and transferred to MongoDB.

Now I would like to add a field to the dashboard in Node Red in which I enter for example the parentID number: urn:epc:id:grai:2 and node red then calls up exactly this entry from the database, with all the associated payload as shown in Figure 1

Unfortunately, it currently looks like this for me: (All entries in the Mongo Db are simply returned)

 ![bsp2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/2/b299c225dbee6817abc49d46175db04e27ef48a1.png)

So I think i would have to specify in a node what exactly it should search for or retrieve... I hope you understand my problem....

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [15 December 2023 20:40 UTC](https://discourse.nodered.org/t/retrieving-certain-data-from-a-mongodb/83741/5 "2023-12-15T20:40:26Z")

</div>

Which mongodb node have you installed (I see several in [flows.nodered.org](http://flows.nodered.org))?  
I don't know anything about mongodb, but presumably you have to build a query that you pass to the database. Do you know what query you need to use to get the records for a particular parentID?

---

<div class="post-metadata">

**Author:** ![Schlitz](https://avatars.discourse-cdn.com/v4/letter/s/3bc359/32.png) [@Schlitz](https://discourse.nodered.org/u/Schlitz)\
**Post date:** [16 December 2023 11:11 UTC](https://discourse.nodered.org/t/retrieving-certain-data-from-a-mongodb/83741/6 "2023-12-16T11:11:27Z")

</div>

I used "mongodb-aleph-in" to get the data back.

It seems that there are possibilities to limit the output values by a function, but unfortunately I don't know how.

![bsp2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/e/6ed638c928df53a23807105c3f5aa8b5047a3e98.png)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [16 December 2023 11:34 UTC](https://discourse.nodered.org/t/retrieving-certain-data-from-a-mongodb/83741/7 "2023-12-16T11:34:12Z")

</div>

Do you mean you don't know what mongodb query you need to run (in which case that isn't a node-red question, and I don't know how to do it), or that you know the query but don't know how to configure msg.payload to contain the query?

---

<div class="post-metadata">

**Author:** ![Schlitz](https://avatars.discourse-cdn.com/v4/letter/s/3bc359/32.png) [@Schlitz](https://discourse.nodered.org/u/Schlitz)\
**Post date:** [16 December 2023 11:50 UTC](https://discourse.nodered.org/t/retrieving-certain-data-from-a-mongodb/83741/8 "2023-12-16T11:50:46Z")

</div>

The query from MongoDB works, but i don't know how to configure msg.payload to contain specific data from the query as described before.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [16 December 2023 12:05 UTC](https://discourse.nodered.org/t/retrieving-certain-data-from-a-mongodb/83741/9 "2023-12-16T12:05:48Z")

</div>

I too know very little about mongodb beyond the fact it gets it's name from being designed to store humongous amounts of data.

But your statements

> [@Schlitz](#):
>
> All entries in the Mongo Db are simply returned

And

> [@Schlitz](#):
>
> The query from MongoDB works

Seem contradictory. .

Provided that you are storing the shipment data correctly in mongodb, there MUST be a way to retrieve just one document.

> [@Schlitz](#):
>
> It seems that there are possibilities to limit the output values by a function,

If you mean a Node-red function to filter the data, that's just plain bonkers.

Surely the dB node you are using allows you to restrict the data found? It's just a question of the right mongodb syntax or database design?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [16 December 2023 12:09 UTC](https://discourse.nodered.org/t/retrieving-certain-data-from-a-mongodb/83741/10 "2023-12-16T12:09:34Z")

</div>

> [@Schlitz](#):
>
> i don't know how to configure msg.payload to contain specific data from the query as described before.

Forget for the moment that you are using node-red, and give us an example of the mongodb query that you want to perform to fetch, for example, the data for urn:epc:id:grai:2.

---

<div class="post-metadata">

**Author:** ![Schlitz](https://avatars.discourse-cdn.com/v4/letter/s/3bc359/32.png) [@Schlitz](https://discourse.nodered.org/u/Schlitz)\
**Post date:** [16 December 2023 12:56 UTC](https://discourse.nodered.org/t/retrieving-certain-data-from-a-mongodb/83741/11 "2023-12-16T12:56:08Z")

</div>

Hi, yes the question is how to restrict it correctly.

My Mongo-DB in node only allows the operation "Find". As a result, I simply get back all the contents of the MongoDB each time I query. But I only want to get back the entries that are together with a specific number that I enter in the dashboard.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/6/466712084fcd51034227f8373ef946c665119914.png)

I have now tried using a function after the mongodb in Node.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/4/549e58309176f2176601368b79718a4748a199e6.png)

Here I was at least able to use msg.protection to limit the fields that I want to have displayed. Now I only get the restricted fields back, but again from all data records and not just those for a specific number.

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

---

<div class="post-metadata">

**Author:** ![Schlitz](https://avatars.discourse-cdn.com/v4/letter/s/3bc359/32.png) [@Schlitz](https://discourse.nodered.org/u/Schlitz)\
**Post date:** [16 December 2023 13:06 UTC](https://discourse.nodered.org/t/retrieving-certain-data-from-a-mongodb/83741/12 "2023-12-16T13:06:21Z")

</div>

Apparently I can use msg.query, but I don't know how to incorporate it into the function.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [16 December 2023 13:29 UTC](https://discourse.nodered.org/t/retrieving-certain-data-from-a-mongodb/83741/13 "2023-12-16T13:29:57Z")

</div>

As I said, please forget about how to do it in node-red for the moment. You need to do some research into the find command in mongodb to work out how to select the data you want.  
To explain more clearly. If you were working using MySQL then you would have to use a query something like

```auto
SELECT * FROM some_table WHERE `parent_id` = "urn:epc:id:grai:2"

```

Once you have worked out what syntax you need in the find command then we can help you get that going in node-red.  
Possibly you should look at using different client for mongodb in order to experiment with it, then once you understand mongodb better then implement it in node-red

---

<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:** [16 December 2023 14:55 UTC](https://discourse.nodered.org/t/retrieving-certain-data-from-a-mongodb/83741/14 "2023-12-16T14:55:30Z")

</div>

From a screenshot you shared of a db document .. we see that your values are nested under `payload`

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

so your query should have been

`msg.query = { "payload.parentID" : parentID}`

---

<div class="post-metadata">

**Author:** ![Schlitz](https://avatars.discourse-cdn.com/v4/letter/s/3bc359/32.png) [@Schlitz](https://discourse.nodered.org/u/Schlitz)\
**Post date:** [16 December 2023 18:01 UTC](https://discourse.nodered.org/t/retrieving-certain-data-from-a-mongodb/83741/15 "2023-12-16T18:01:18Z")

</div>

I have now customised the function as follows.  
"searchvalue" should be the value of the msg.payload that I enter in the dashboard.

I have set it so that initially only the parentIDs are displayed. Nevertheless, all parentIDs are displayed when I enter "urn:epc:id:grai:333" in the dashboard field.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/b/dbd1a8da783452834f96d3db2603e9c95e7f61fa.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/3/8344c1c0ffc71adaee6c2d977834ea1e67f9a86d.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:** [16 December 2023 18:14 UTC](https://discourse.nodered.org/t/retrieving-certain-data-from-a-mongodb/83741/16 "2023-12-16T18:14:50Z")

</div>

Reading the Documentation of the node a bit more closely it mentions that

> _Find_ queries a collection using the `msg.payload` as the query statement as per the _.find()_ function.

So using `msg.query` in your code wont have any effect for find().

Try `msg.payload = { "payload.parentID" : searchValue}`

---

<div class="post-metadata">

**Author:** ![Schlitz](https://avatars.discourse-cdn.com/v4/letter/s/3bc359/32.png) [@Schlitz](https://discourse.nodered.org/u/Schlitz)\
**Post date:** [16 December 2023 18:21 UTC](https://discourse.nodered.org/t/retrieving-certain-data-from-a-mongodb/83741/17 "2023-12-16T18:21:10Z")

</div>

I had already tried this, but unfortunately nothing changes...

(In the picture now only using a inject node to create msg.payload with the value of "urn:epc:id:grai:333")

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/8/a8d064f8353bdc8801ea36c159f3c5f0a599adf7.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:** [16 December 2023 21:32 UTC](https://discourse.nodered.org/t/retrieving-certain-data-from-a-mongodb/83741/18 "2023-12-16T21:32:49Z")

</div>

> [@Schlitz](#):
>
> but unfortunately nothing changes...

you mean you still get all the db records without any filtering done ?

I tried those `mongodb-aleph` nodes on my test mongodb and having the query in `msg.payload` works for me.

Can you put a Debug node and show us the msg coming out of **Function 7**?  
Can you double check if your Operation in the node config is set to "find"  
~~Could it be that your NR is set to German language and Find operation shows up as "Finden" .. could that interfere with the Node's logic ?~~

---

<div class="post-metadata">

**Author:** ![Schlitz](https://avatars.discourse-cdn.com/v4/letter/s/3bc359/32.png) [@Schlitz](https://discourse.nodered.org/u/Schlitz)\
**Post date:** [16 December 2023 22:54 UTC](https://discourse.nodered.org/t/retrieving-certain-data-from-a-mongodb/83741/19 "2023-12-16T22:54:19Z")

</div>

Here is a screenshot of my Node Red Flows to retrieve the data:

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

I have temporarily replaced the text input field with an inject node, which generates an msg.payload with a query value as an example. (later the msg.payload should, in the best case, be able to be entered variably in the dashboard)

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

This is followed by the function:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/2/a2b26d34cf26cb964ab6e2b05fd82c4ee5619c9a.png)

With the current settings, I no longer get any values back:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/b/4b5163442dcb318bc9fcad1b6c8c1efce96493e2.png)

---

<div class="post-metadata">

**Author:** ![Schlitz](https://avatars.discourse-cdn.com/v4/letter/s/3bc359/32.png) [@Schlitz](https://discourse.nodered.org/u/Schlitz)\
**Post date:** [16 December 2023 23:00 UTC](https://discourse.nodered.org/t/retrieving-certain-data-from-a-mongodb/83741/20 "2023-12-16T23:00:04Z")

</div>

But maybe it's also because I save the values differently than I want to retrieve them now... As I said, I am a beginner in the field of Node Red...

My input is done through 8 input fields on the Node Red dashboard, which are then bundeled together before I save them in MongoDB through a join node.

Do I need to address the data differently to retrieve it?

Example text\_input:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/6/56ca80f10e677f70d184a94739706bef2a86ea14.png)

Join Node:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/8/d830e02192d18898a2c6f71ef5f6983c66aabf69.png)

[Next page](https://discourse.nodered.org/t/retrieving-certain-data-from-a-mongodb/83741.md?page=2)
