Retrieving certain data from a MongoDB

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 :smiley:
Thanks in advance!

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.

1 Like

What do you mean by that?

bsp

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)

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....

Which mongodb node have you installed (I see several in 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?

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

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?

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.

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

And

Seem contradictory. .

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

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?

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.

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

I have now tried using a function after the mongodb in Node.
image

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

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

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

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

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

image

so your query should have been

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

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
image

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}

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")

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 ?

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

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

This is followed by the function:
image

With the current settings, I no longer get any values back:
image

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:

Join Node:
image