ozomer / node-red-contrib-mongodb2

MongoDB driver node for Node-RED
Apache License 2.0
15 stars 19 forks source link

Helping hand with Inject Node #19

Closed jjpeet closed 6 years ago

jjpeet commented 6 years ago

Hi, I'm new to Node Red & Mongo, and am working on some IoT/Home Automation projects. Current;y I'm writing a few HomeAssistant sensors to MongoDB from MQTT > NodeRed > a few custom data changes > Mongo.

Now I'm trying to figure out how to get the data out of Mongo. I've been trying the other mongo nodes, but they won't let me use date & string functions so was hoping to use your node instead which looks like it will be much more flexible as it gives me access to the full mongoDB API.

I was wondering if you could provide an example of what I should pass into MongoDB2? I assume I should use the inject node, and put some API code in there? I just need some help starting me off.

Thx JP

ozomer commented 6 years ago

All you have to do is to send a msg where msg.payload is an array of parameters that will be passed to the relevant mongodb operation. For example, the findOne operation accepts two parameters: query and options (and an optional callback that you should ignore). So, for example, you can make a function node that does:

msg.payload = [{"_id": "hello"}, {}];
return msg

and connect it to a node-red-contrib-mongodb2 node. When the function node sends this msg, it will find the "hello" document.

You can see all other operations in the link. node-red-contrib-mongodb2 just wraps them. You can also look at the code, it is less complex than you might think.

I hope I've helped. Oren

jjpeet commented 6 years ago

Thanks Oren,

So this is what I've put into a function node; msg.payload = [{"event_data.entity_id": "sensor.power_costs_today"}, {}]; return msg

I hook it up the the MongoDB2 node (findOne) which then goes to debug node.

Then I hook up an input node set to timestamp to the left of the function node to make it fire by clicking the left hand button - not sure if that's the right approach.

When I click, the result is Null whereas if I do a find for these documents from the command line - db.EnergySensors.find({"event_data.entity_id": "sensor.power_costs_today"});) - I get 75 documents returned.

What am I doing wrong?

Thx James

On 11 January 2018 at 22:13, Oren Zomer notifications@github.com wrote:

All you have to do is to send a msg where msg.payload is an array of parameters that will be passed to the relevant mongodb operation. For example, the findOne http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#findOne operation accepts two parameters: query and options (and an optional callback that you should ignore). So, for example, you can make a function node that does:

msg.payload = [{"_id": "hello"}, {}]; return msg

and connect it to a node-red-contrib-mongodb2 node. When the function node sends this msg, it will find the "hello" document.

You can see all other operations in the link. node-red-contrib-mongodb2 just wraps them. You can also look at the code, it is less complex than you might think.

I hope I've helped. Oren

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ozomer/node-red-contrib-mongodb2/issues/19#issuecomment-356917041, or mute the thread https://github.com/notifications/unsubscribe-auth/AGuCfqbXcaEe7zXa4KGMkJt_T9gQPMvPks5tJfsDgaJpZM4RXIfd .

-- James Peet http://www.jjpeet.com http://www.linkedin.com/in/jamesjpeet

ozomer commented 6 years ago

Not sure, Maybe you misspelled to name of the collection in the configuration of the mongodb2 node?

Regards, Oren

On Tue, Jan 16, 2018 at 9:05 AM James Peet notifications@github.com wrote:

Thanks Oren,

So this is what I've put into a function node; msg.payload = [{"event_data.entity_id": "sensor.power_costs_today"}, {}]; return msg

I hook it up the the MongoDB2 node (findOne) which then goes to debug node.

Then I hook up an input node set to timestamp to the left of the function node to make it fire by clicking the left hand button - not sure if that's the right approach.

When I click, the result is Null whereas if I do a find for these documents from the command line - db.EnergySensors.find({"event_data.entity_id": "sensor.power_costs_today"});) - I get 75 documents returned.

What am I doing wrong?

Thx James

On 11 January 2018 at 22:13, Oren Zomer notifications@github.com wrote:

All you have to do is to send a msg where msg.payload is an array of parameters that will be passed to the relevant mongodb operation. For example, the findOne < http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#findOne

operation accepts two parameters: query and options (and an optional callback that you should ignore). So, for example, you can make a function node that does:

msg.payload = [{"_id": "hello"}, {}]; return msg

and connect it to a node-red-contrib-mongodb2 node. When the function node sends this msg, it will find the "hello" document.

You can see all other operations in the link. node-red-contrib-mongodb2 just wraps them. You can also look at the code, it is less complex than you might think.

I hope I've helped. Oren

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub < https://github.com/ozomer/node-red-contrib-mongodb2/issues/19#issuecomment-356917041 , or mute the thread < https://github.com/notifications/unsubscribe-auth/AGuCfqbXcaEe7zXa4KGMkJt_T9gQPMvPks5tJfsDgaJpZM4RXIfd

.

-- James Peet http://www.jjpeet.com http://www.linkedin.com/in/jamesjpeet

— You are receiving this because you modified the open/close state.

Reply to this email directly, view it on GitHub https://github.com/ozomer/node-red-contrib-mongodb2/issues/19#issuecomment-357871229, or mute the thread https://github.com/notifications/unsubscribe-auth/ABG4LAR6sJ2VQSLdCsFXEs-WA6K-m-BGks5tLEo6gaJpZM4RXIfd .

--

Oren Zomer Co-Founder & CTO at Awear Solutions Cell: +972-545399470 <+972%2054-539-9470>

jjpeet commented 6 years ago

Hi Oren,

Nope - it's set correctly. Any other thoughts?

Thanks JP

On 16 January 2018 at 17:27, Oren Zomer notifications@github.com wrote:

Not sure, Maybe you misspelled to name of the collection in the configuration of the mongodb2 node?

Regards, Oren

On Tue, Jan 16, 2018 at 9:05 AM James Peet notifications@github.com wrote:

Thanks Oren,

So this is what I've put into a function node; msg.payload = [{"event_data.entity_id": "sensor.power_costs_today"}, {}]; return msg

I hook it up the the MongoDB2 node (findOne) which then goes to debug node.

Then I hook up an input node set to timestamp to the left of the function node to make it fire by clicking the left hand button - not sure if that's the right approach.

When I click, the result is Null whereas if I do a find for these documents from the command line - db.EnergySensors.find({"event_data.entity_id": "sensor.power_costs_today"});) - I get 75 documents returned.

What am I doing wrong?

Thx James

On 11 January 2018 at 22:13, Oren Zomer notifications@github.com wrote:

All you have to do is to send a msg where msg.payload is an array of parameters that will be passed to the relevant mongodb operation. For example, the findOne < http://mongodb.github.io/node-mongodb-native/2.2/api/ Collection.html#findOne

operation accepts two parameters: query and options (and an optional callback that you should ignore). So, for example, you can make a function node that does:

msg.payload = [{"_id": "hello"}, {}]; return msg

and connect it to a node-red-contrib-mongodb2 node. When the function node sends this msg, it will find the "hello" document.

You can see all other operations in the link. node-red-contrib-mongodb2 just wraps them. You can also look at the code, it is less complex than you might think.

I hope I've helped. Oren

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub < https://github.com/ozomer/node-red-contrib-mongodb2/ issues/19#issuecomment-356917041 , or mute the thread < https://github.com/notifications/unsubscribe- auth/AGuCfqbXcaEe7zXa4KGMkJt_T9gQPMvPks5tJfsDgaJpZM4RXIfd

.

-- James Peet http://www.jjpeet.com http://www.linkedin.com/in/jamesjpeet

— You are receiving this because you modified the open/close state.

Reply to this email directly, view it on GitHub https://github.com/ozomer/node-red-contrib-mongodb2/ issues/19#issuecomment-357871229, or mute the thread https://github.com/notifications/unsubscribe- auth/ABG4LAR6sJ2VQSLdCsFXEs-WA6K-m-BGks5tLEo6gaJpZM4RXIfd .

--

Oren Zomer Co-Founder & CTO at Awear Solutions Cell: +972-545399470 <+972%2054-539-9470> <+972%2054-539-9470>

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ozomer/node-red-contrib-mongodb2/issues/19#issuecomment-357875044, or mute the thread https://github.com/notifications/unsubscribe-auth/AGuCfq-MTgi2I-0_BprMn8wLbEHlWTkSks5tLE9kgaJpZM4RXIfd .

-- James Peet http://www.jjpeet.com http://www.linkedin.com/in/jamesjpeet

ozomer commented 6 years ago

Hi JP, Try doing an insertOne operation and see if a new document is inserted. I also suggest to look at the logs.

Regards, Oren On Wed, 17 Jan 2018 at 5:27 James Peet notifications@github.com wrote:

Hi Oren,

Nope - it's set correctly. Any other thoughts?

Thanks JP

On 16 January 2018 at 17:27, Oren Zomer notifications@github.com wrote:

Not sure, Maybe you misspelled to name of the collection in the configuration of the mongodb2 node?

Regards, Oren

On Tue, Jan 16, 2018 at 9:05 AM James Peet notifications@github.com wrote:

Thanks Oren,

So this is what I've put into a function node; msg.payload = [{"event_data.entity_id": "sensor.power_costs_today"}, {}]; return msg

I hook it up the the MongoDB2 node (findOne) which then goes to debug node.

Then I hook up an input node set to timestamp to the left of the function node to make it fire by clicking the left hand button - not sure if that's the right approach.

When I click, the result is Null whereas if I do a find for these documents from the command line - db.EnergySensors.find({"event_data.entity_id": "sensor.power_costs_today"});) - I get 75 documents returned.

What am I doing wrong?

Thx James

On 11 January 2018 at 22:13, Oren Zomer notifications@github.com wrote:

All you have to do is to send a msg where msg.payload is an array of parameters that will be passed to the relevant mongodb operation. For example, the findOne < http://mongodb.github.io/node-mongodb-native/2.2/api/ Collection.html#findOne

operation accepts two parameters: query and options (and an optional callback that you should ignore). So, for example, you can make a function node that does:

msg.payload = [{"_id": "hello"}, {}]; return msg

and connect it to a node-red-contrib-mongodb2 node. When the function node sends this msg, it will find the "hello" document.

You can see all other operations in the link. node-red-contrib-mongodb2 just wraps them. You can also look at the code, it is less complex than you might think.

I hope I've helped. Oren

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub < https://github.com/ozomer/node-red-contrib-mongodb2/ issues/19#issuecomment-356917041 , or mute the thread < https://github.com/notifications/unsubscribe- auth/AGuCfqbXcaEe7zXa4KGMkJt_T9gQPMvPks5tJfsDgaJpZM4RXIfd

.

-- James Peet http://www.jjpeet.com http://www.linkedin.com/in/jamesjpeet

— You are receiving this because you modified the open/close state.

Reply to this email directly, view it on GitHub https://github.com/ozomer/node-red-contrib-mongodb2/ issues/19#issuecomment-357871229, or mute the thread https://github.com/notifications/unsubscribe- auth/ABG4LAR6sJ2VQSLdCsFXEs-WA6K-m-BGks5tLEo6gaJpZM4RXIfd .

--

Oren Zomer Co-Founder & CTO at Awear Solutions Cell: +972-545399470 <+972%2054-539-9470> <+972%2054-539-9470>

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub < https://github.com/ozomer/node-red-contrib-mongodb2/issues/19#issuecomment-357875044 , or mute the thread < https://github.com/notifications/unsubscribe-auth/AGuCfq-MTgi2I-0_BprMn8wLbEHlWTkSks5tLE9kgaJpZM4RXIfd

.

-- James Peet http://www.jjpeet.com http://www.linkedin.com/in/jamesjpeet

— You are receiving this because you modified the open/close state.

Reply to this email directly, view it on GitHub https://github.com/ozomer/node-red-contrib-mongodb2/issues/19#issuecomment-358185891, or mute the thread https://github.com/notifications/unsubscribe-auth/ABG4LDGAOLaDtkrcFdKMTyMVoa31axx2ks5tLWi7gaJpZM4RXIfd .

--

Oren Zomer Co-Founder & CTO at Awear Solutions Cell: +972-545399470 <+972%2054-539-9470>