Using MongoDB in a Flow
If you want fast data access or easy web request caching, you can assign the database MongoDB to a flow. By default, flows do not use a database. MongoDB is currently the only type of database supported in Flows.
Follow these steps to assign MongoDB to a flow:
1. | From the Developer Console, click the down arrow to the right of the flow to open the Admin menu, and then select "Edit databases".![]() |
2. | In the Edit database access dialog box, from the Unassigned databases field drop-down list, select "mongo" and then click Assign to this flow.![]() |
3. | Click Close. |
4. | On the Developer Console, "mongo" is now listed under databases for this flow.![]() |
If you have assigned MongoDB to a flow, you can use the mongo node within this flow. The mongo node is available in the storage section of the Flow Editor's nodes palette.
To use the mongo node, drag it onto a sheet within the Flow Editor and double-click on the node.
From the Edit mongo node dialog box, you can edit the following settings:
• | Name - The name of the mongo node. |
• | Config - Select an existing configuration or click on the edit icon ![]() |
• | Operation - Select one of the supported operations. |
• | Insert output into msg.x - Replace x with the name of the message property that will be passed to the next node. |
• | Use streaming? - Select this check box to use streaming for this MongoDB operation. |
Alternatively, you can add a mongo operation to the msg property of another flow node, when you pass the msg object to a mongo node. For example, you could add the following line to a function node:
msg.mongoOperation = 'insertOne';
The mongo node supports the following operations, which are all performed on the collection that is defined in the Config of the mongo node or within another flow node, for example a function node. Any filters that define which object, or objects, to perform the operation on are defined in the function node that precedes the mongo node, or that contains the operation message.
• | Insert One: Inserts an object into a collection. MongoDB automatically allocates an ID for the object. |
• | Insert Many: Inserts an array of objects into a collection. |
• | Find One: Finds an object that matches a filter in a collection. |
• | Find All: Finds all objects in a collection. Filtering is currently not supported for this operation. |
• | Update One: Updates a record in the database. The filter property defines which object to search for. A doc (document) property defines the changes to make. An update can be a simple replace or a more advanced $set option. |
• | Update Many: Updates several records in the database. The filter property defines which objects to search for. The doc (document) property defines what to update in the records. An update can be a simple replace or a more advanced $set option. |
• | Delete One: Deletes an object from a collection. The filter property defines which object to delete. |
• | Delete Many: Deletes several objects from a collection. The filter property defines which objects to delete. |
For more information on using collections within MongoDB see the MongoDB help.
The following example shows a flow with these nodes:
• | A function node that defines a "filter" and "doc" for the data to update in the MongoDB. |
• | A function node that sets the msg.payload. |
• | A mongo node with an operation of "Update One". |
• | A mongo node with an operation of "Find All". |
• | A mongo node with an operation of "Insert One". |
• | A function node that sets the msg.payload. |
• | A mongo node with an operation of "Find One". |
• | A function node that sets the msg.payload. |
• | A mongo node with an operation of "Delete Many". |
• | A function node that defines a filter and doc for the data to update in the MongoDB. |
• | A mongo node with an operation of "Update Many". |
• | A mongo-config node that defines the MongoDB collection to use. |
[
{
"id": "31e76c72.c08384",
"type": "function",
"z": "292e1666.38bc5a",
"name": "",
"func": "msg.payload = {\n
filter: {\n
data: 'add some data in here'\n
},\n
doc: {\n
data: 'add some data in here',\n
moreData: 'look at all the data here' \n
}\n
};
\n
\n
\n
\n
return msg;",
"outputs": 1,
"noerr": 0,
"x": 290,
"y": 280,
"wires": [
[
"7b9d6217.7f36cc"
]
]
},
{
"id": "57377a3e.805b94",
"type": "inject",
"z": "292e1666.38bc5a",
"name": "",
"topic": "",
"payload": "",
"payloadType": "date",
"crontab": "",
"timezone": "",
"once": false,
"x": 140,
"y": 220,
"wires": [
[
"9a4cbbed.5472d8"
]
]
},
{
"id": "673e938e.4da5cc",
"type": "function",
"z": "292e1666.38bc5a",
"name": "",
"func": "msg.payload = {\n
data: 'add some data in here'\n}
;\n
\n
\n
return msg;",
"outputs": 1,
"noerr": 0,
"x": 290,
"y": 100,
"wires": [
[
"d5d42af8.4deb98"
]
]
},
{
"id": "7b9d6217.7f36cc",
"type": "mongo-connector",
"z": "292e1666.38bc5a",
"name": "",
"config": "1a0891f7.5e4bce",
"operation": "updateOne",
"outputDataLocation": "",
"x": 470,
"y": 280,
"wires": [
[
"a5ad689e.b5c8a8"
]
]
},
{
"id": "9a4cbbed.5472d8",
"type": "mongo-connector",
"z": "292e1666.38bc5a",
"name": "",
"config": "1a0891f7.5e4bce",
"operation": "findAll",
"outputDataLocation": "",
"x": 460,
"y": 220,
"wires": [
[
"ef43d7e3.4fad38"
]
]
},
{
"id": "a5ad689e.b5c8a8",
"type": "debug",
"z": "292e1666.38bc5a",
"name": "",
"active": true,
"console": "false",
"complete": "false",
"x": 670,
"y": 280,
"wires": []
},
{
"id": "c9521cc3.8ef0e",
"type": "debug",
"z": "292e1666.38bc5a",
"name": "",
"active": true,
"console": "false",
"complete": "false",
"x": 670,
"y": 100,
"wires": []
},
{
"id": "d5d42af8.4deb98",
"type": "mongo-connector",
"z": "292e1666.38bc5a",
"name": "",
"config": "1a0891f7.5e4bce",
"operation": "insertOne",
"outputDataLocation": "",
"x": 470,
"y": 100,
"wires": [
[
"c9521cc3.8ef0e"
]
]
},
{
"id": "e051bb31.8184d8",
"type": "inject",
"z": "292e1666.38bc5a",
"name": "",
"topic": "",
"payload": "",
"payloadType": "date",
"crontab": "",
"timezone": "",
"once": false,
"x": 140,
"y": 280,
"wires": [
[
"31e76c72.c08384"
]
]
},
{
"id": "ef43d7e3.4fad38",
"type": "debug",
"z": "292e1666.38bc5a",
"name": "",
"active": true,
"console": "false",
"complete": "false",
"x": 670,
"y": 220,
"wires": []
},
{
"id": "ffe5e3ed.8f55b",
"type": "inject",
"z": "292e1666.38bc5a",
"name": "",
"topic": "",
"payload": "",
"payloadType": "date",
"crontab": "",
"timezone": "",
"once": false,
"x": 140,
"y": 100,
"wires": [
[
"673e938e.4da5cc"
]
]
},
{
"id": "37f80e46.3ace12",
"type": "function",
"z": "292e1666.38bc5a",
"name": "",
"func": "msg.payload = {\n
data: 'add some data in here'\n}
;\n
\n
\n
\n
return msg;",
"outputs": 1,
"noerr": 0,
"x": 290,
"y": 160,
"wires": [
[
"a137c7fb.42ea38"
]
]
},
{
"id": "a137c7fb.42ea38",
"type": "mongo-connector",
"z": "292e1666.38bc5a",
"name": "",
"config": "1a0891f7.5e4bce",
"operation": "findOne",
"outputDataLocation": "payload",
"x": 460,
"y": 160,
"wires": [
[
"edf11567.e86cd8"
]
]
},
{
"id": "edf11567.e86cd8",
"type": "debug",
"z": "292e1666.38bc5a",
"name": "",
"active": true,
"console": "false",
"complete": "false",
"x": 670,
"y": 160,
"wires": []
},
{
"id": "ce8cccf6.fd8ca",
"type": "inject",
"z": "292e1666.38bc5a",
"name": "",
"topic": "",
"payload": "",
"payloadType": "date",
"crontab": "",
"timezone": "",
"once": false,
"x": 140,
"y": 160,
"wires": [
[
"37f80e46.3ace12"
]
]
},
{
"id": "57a1e744.458298",
"type": "function",
"z": "292e1666.38bc5a",
"name": "",
"func": "msg.payload = {\n
};\n
\n
\n
\n
return msg;",
"outputs": 1,
"noerr": 0,
"x": 290,
"y": 400,
"wires": [
[
"f3c536a5.6732a8"
]
]
},
{
"id": "f3c536a5.6732a8",
"type": "mongo-connector",
"z": "292e1666.38bc5a",
"name": "",
"config": "1a0891f7.5e4bce",
"operation": "deleteMany",
"outputDataLocation": "payload",
"x": 470,
"y": 400,
"wires": [
[
"2b919f27.ce432"
]
]
},
{
"id": "2b919f27.ce432",
"type": "debug",
"z": "292e1666.38bc5a",
"name": "",
"active": true,
"console": "false",
"complete": "false",
"x": 670,
"y": 400,
"wires": []
},
{
"id": "3ed517fd.cf0c08",
"type": "inject",
"z": "292e1666.38bc5a",
"name": "",
"topic": "",
"payload": "",
"payloadType": "date",
"crontab": "",
"timezone": "",
"once": false,
"x": 140,
"y": 400,
"wires": [
[
"57a1e744.458298"
]
]
},
{
"id": "7ff92b85.da4284",
"type": "function",
"z": "292e1666.38bc5a",
"name": "",
"func": "msg.payload = {\n
filter: {\n
data: 'add some data in here'\n
},\n
doc: {\n
\"$set\": {\n
moreData: 'look at all the data here' \n
}\n
}\n
};\n
\n
\n
\n
return msg;",
"outputs": 1,
"noerr": 0,
"x": 290,
"y": 340,
"wires": [
[
"3ec161f7.440dfe"
]
]
},
{
"id": "3ec161f7.440dfe",
"type": "mongo-connector",
"z": "292e1666.38bc5a",
"name": "",
"config": "1a0891f7.5e4bce",
"operation": "updateMany",
"outputDataLocation": "payload",
"x": 480,
"y": 340,
"wires": [
[
"1e4eff3f.4564d1"
]
]
},
{
"id": "1e4eff3f.4564d1",
"type": "debug",
"z": "292e1666.38bc5a",
"name": "",
"active": true,
"console": "false",
"complete": "false",
"x": 670,
"y": 340,
"wires": []
},
{
"id": "94fbfb8e.996428",
"type": "inject",
"z": "292e1666.38bc5a",
"name": "",
"topic": "",
"payload": "",
"payloadType": "date",
"crontab": "",
"timezone": "",
"once": false,
"x": 140,
"y": 340,
"wires": [
[
"7ff92b85.da4284"
]
]
},
{
"id": "1a0891f7.5e4bce",
"type": "mongo-config",
"z": "",
"collection": "test"
}
]