2015-06-13 19:47:00 +02:00
node-red-node-mongodb
=====================
A < a href = "http://nodered.org" target = "_new" > Node-RED< / a > node to save data in a MongoDB database.
2015-06-16 22:37:22 +02:00
**Note** : This is the same node as was in the core of Node-RED.
As of v0.10.8 you will need to install it from here if still required.
2015-06-13 19:47:00 +02:00
Pre-requisite
-------------
To run this you need a local MongoDB server running. For details see
< a href = "https://www.mongodb.org/" target = "_new" > the MongoDB site< / a > .
Install
-------
2016-03-02 14:27:22 +01:00
Run the following command in your Node-RED user directory - typically `~/.node-red`
2020-09-14 18:21:41 +02:00
```
npm install node-red-node-mongodb
```
2021-01-22 14:50:37 +01:00
Note that this package requires a MongoDB client package at least version 3.6.3 - if you have an older (version 2) client,
2020-09-14 18:21:41 +02:00
you may need to remove that before installing this
```
npm remove mongodb
2015-06-13 19:47:00 +02:00
npm install node-red-node-mongodb
2020-09-14 18:21:41 +02:00
```
2015-06-13 19:47:00 +02:00
Usage
-----
2021-01-22 14:50:37 +01:00
Nodes to save and retrieve data in a MongoDB instance - the database server can be local (mongodb//:localhost:27017), remote (mongodb://hostname.network:27017),
replica-set or cluster (mongodb://hostnameA.network:27017,hostnameB.network:27017), and DNS seedlist cluster (mongodb+srv://clustername.network).
2020-09-14 18:21:41 +02:00
2021-01-22 14:50:37 +01:00
Reference [MongoDB docs ](https://docs.mongodb.com/manual/reference/connection-string/ ) to see which connection method (host or clustered) to use for your MongoDB instance.
2015-06-13 19:47:00 +02:00
2016-03-02 14:27:22 +01:00
### Input
2015-06-13 19:47:00 +02:00
Calls a MongoDB collection method based on the selected operator.
2016-03-02 14:27:22 +01:00
*Find* queries a collection using the `msg.payload` as the query statement as
2015-06-13 19:47:00 +02:00
per the *.find()* function.
Optionally, you may also (via a function) set
2016-03-02 14:27:22 +01:00
- a `msg.projection` object to constrain the returned fields,
- a `msg.sort` object,
- a `msg.limit` number,
- a `msg.skip` number.
2015-06-13 19:47:00 +02:00
*Count* returns a count of the number of documents in a collection or matching a
2016-03-02 14:27:22 +01:00
query using the `msg.payload` as the query statement.
2015-06-13 19:47:00 +02:00
2016-03-02 14:27:22 +01:00
*Aggregate* provides access to the aggregation pipeline using the `msg.payload` as the pipeline array.
2015-06-13 19:47:00 +02:00
2016-03-02 14:27:22 +01:00
You can either set the collection method in the node config or on `msg.collection` .
Setting it in the node will override `msg.collection` .
2015-06-13 19:47:00 +02:00
See the < a href = "http://docs.mongodb.org/manual/reference/method/db.collection.find/" target = "new" > *MongoDB collection methods docs*< / a > for examples.
2016-03-02 14:27:22 +01:00
The result is returned in `msg.payload` .
2015-06-13 19:47:00 +02:00
2016-03-02 14:27:22 +01:00
### Output
2015-06-13 19:47:00 +02:00
A simple MongoDB output node. Can save, insert, update and remove objects from a chosen collection.
MongoDB only accepts objects.
2016-03-02 14:27:22 +01:00
Save and insert can either store `msg` or `msg.payload` . If msg.payload is
2015-06-13 19:47:00 +02:00
selected it should contain an object. If not it will be wrapped in an object with a name of payload.
*Save* will update an existing object or insert a new object if one does not already exist.
*Insert* will insert a new object.
*Update* will modify an existing object or objects. The query to select objects
2016-03-02 14:27:22 +01:00
to update uses `msg.query` and the update to the element uses `msg.payload` .
2015-12-09 21:12:17 +01:00
Update can add an object if it does not exist or update multiple objects.
2015-06-13 19:47:00 +02:00
2016-03-02 14:27:22 +01:00
*Remove* will remove objects that match the query passed in on `msg.payload` .
2015-06-13 19:47:00 +02:00
A blank query will delete *all of the objects* in the collection.
2016-03-02 14:27:22 +01:00
You can either set the collection method in the node config or on `msg.collection` .
Setting it in the node will override `msg.collection` .
2015-06-13 19:47:00 +02:00
2016-03-02 14:27:22 +01:00
By default MongoDB creates an `msg._id` property as the primary key - so
repeated injections of the same `msg` will result in many database entries.
2015-06-13 19:47:00 +02:00
If this is NOT the desired behaviour - ie. you want repeated entries to overwrite,
2016-03-02 14:27:22 +01:00
then you must set the `msg._id` property to be a constant by the use of a previous function node.
2015-06-13 19:47:00 +02:00
This must be done at the correct level. If only writing msg.payload then payload must contain the \_id property.
If writing the whole msg object then it must contain an \_id property.
This could be a unique constant or you could create one based on some other msg property.
Currently we do not limit or cap the collection size at all...