ozomer / node-red-contrib-mongodb2

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

How to get the db or collection instance #5

Closed wstam88 closed 8 years ago

wstam88 commented 8 years ago

Hi,

I'm having trouble to get the DB or collection instance; when I select the db or collection from the list where does it go? How can I get access to it?

Thanks,

Wesley

ozomer commented 8 years ago

The db\collection will be placed in msg.payload, but node-red-contrib-mongodb2 is mostly used for doing operations on mongodb and not for fetching the db instance. In order to do a specific operation, pass its arguments in an array in the input message payload. For example, try calling msg.payload = [{_id:'x'},{fields:{myfield:1}}]; in a function node that is wired to a node-red-contrib-mongodb2 node with a findOne operation.

wstam88 commented 8 years ago

Thank you for your answer, however I find that your example doesn't work for me. It is my understanding that in order to find documents by there ID it has to be something like this: '{"_id": ObjectId(ID)}'. So, the ID field is special in MongoDB unless it was set by hand. It does however work for other fields.

The examples on http://flows.nodered.org do not query based on a MongoDB generated _id which stores itself with a type of 'ObjectID'.

Any suggestions?

ozomer commented 8 years ago

Hi, In my server I added mongodb: require('mongodb') to the red_settings' functionGlobalContext. Then I can use global.get('mongodb').ObjectId(...) in any function node.

wstam88 commented 8 years ago

Thank you, that seems like a great solution.