MongoDb
Overview
MongoDb step is used to access MongoDb and make basic queries on the database (inserting, selecting, updating and deleting)
Configuration
Connection string
- the MongoDb connection string (the recommended way of usage would be to specify a global connection string and access it here through e.g.{{$env.MONGODB_CONNECTION}}
)database name
- the mongodb database to connect toConfiguration
- list of queries to be madeaction
- the action to preform - currently supported areinsert
,find
,update
,delete
andaggregate
methodscollection name
- the name of the collection on which to execute the querytarget path
- thedoc
node on which to attach the result of querypayload
- used for insert query to define payload to be inserted into collectionquery
- the query to preform on collection (used forfind
,delete
andaggregate
)options
- used forfind
method - additional sorting options for queryfilter
- used forupdate
method - the filter used to determine which documents to updateupdated item
- the update payload
Find syntax differs from Mongo shell, see Node.js driver documentation
If you are using ace version above 4.8.6, please use the MongoDB version 4 and above.
MongoDB ObjectId
If query
contains references to mongodb
internal ids that are using ObjectId
special syntax has to be used.
For example query like this:
{ "_id" : "ObjectId("5e07158c25ddae1f53b621fd")" }
has to be used in this way:
{ "_id" : "$ObjectId(5e07158c25ddae1f53b621fd)" }
Essentially calls to mongodb ObjectId("<id>")
has to be replaced with $ObjectId(<id>)
.
Examples
Insert
{
"stepType": "mongo-db",
"color": "rgb(95, 206, 165)",
"displayName": "MongoDB",
"isSelected": true,
"config": {
"actions": [
{
"action": "insert",
"query": {},
"options": {
"sort": {},
"projection": {}
},
"filter": {},
"payload": [
{
"title": "The New Document",
"body": "this is a short new document"
}
],
"updateDoc": {
"$set": {}
},
"collection": "test",
"target": "result"
}
]
},
"connection": "{{$env.MONGODB_CONNECTION}}",
"database": "demo",
"description": "insert documents into collection"
}
{
"doc": {
"result": {
"acknowledged": true,
"insertedCount": 1,
"insertedIds": {
"0": "62bc695ecba369afc6709388"
}
}
},
"errors": [],
"performance": {
"steps": [
{
"step": "mongo-db",
"executionTime": 61
}
],
"executionTimeOfFlow": 61,
"timeMetric": "ms"
}
}
Find
If query
contains references to mongodb
internal ids that are using ObjectId
special syntax has to be used.
For example query like this:
{ "_id" : "ObjectId("5e07158c25ddae1f53b621fd")" }
has to be used in this way:
{ "_id" : "$ObjectId(5e07158c25ddae1f53b621fd)" }
Calls to mongodb ObjectId("<id>")
has to be replaced with $ObjectId(<id>)
.
{
"stepType": "mongo-db",
"color": "rgb(95, 206, 165)",
"displayName": "MongoDB",
"isSelected": true,
"config": {
"actions": [
{
"action": "find",
"query": {},
"options": {
"sort": {},
"projection": {}
},
"filter": {},
"payload": [],
"updateDoc": {
"$set": {}
},
"collection": "test",
"target": "result"
}
]
},
"connection": "{{$env.MONGODB_CONNECTION}}",
"database": "demo",
"description": ""
}
{
"doc": {
"result": [
{
"_id": "62bc695ecba369afc6709388",
"title": "The New Document",
"body": "this is a short new document"
}
]
},
"errors": [],
"performance": {
"steps": [
{
"step": "mongo-db",
"executionTime": 12
}
],
"executionTimeOfFlow": 14,
"timeMetric": "ms"
}
}
Find by id
{
"flow": {
"name": "exampleMongoDb",
"description": "",
"steps": [
{
"stepType": "mongo-db",
"color": "rgb(95, 206, 165)",
"displayName": "MongoDB",
"isSelected": true,
"config": {
"actions": [
{
"action": "find",
"query": {
"_id": "$ObjectId(651e9efbc8fba1d0fa66dba6)"
},
"options": {
"sort": {},
"projection": {}
},
"filter": {},
"payload": [],
"updateDoc": {
"$set": {}
},
"collection": "test",
"target": "result"
}
]
},
"connection": "{{$env.MONGODB_CONNECTION}}",
"database": "demo",
"description": "query collection"
}
]
},
"name": "exampleMongoDb",
"inputSchema": {},
"tags": ["example"],
"inputSchemaLabel": "",
"version": 1,
"id": "2b2afba2-2547-42fb-b703-9bb652e3f775",
"createDate": "2021-10-10T06:35:21.025Z"
}
{
"doc": {
"result": [
{
"_id": "651e9efbc8fba1d0fa66dba6",
"item": "something"
}
]
},
"errors": [],
"performance": {
"steps": [
{
"step": "mongo-db",
"executionTime": 57
}
],
"executionTimeOfFlow": 58,
"timeMetric": "ms"
}
}
Find with $or
{
"flow": {
"name": "exampleFindOrMongoDb",
"description": "",
"steps": [
{
"stepType": "mongo-db",
"color": "rgb(95, 206, 165)",
"displayName": "MongoDB",
"isSelected": true,
"config": {
"actions": [
{
"action": "find",
"query": {
"$or": [
{ "_id": "$ObjectId(651e9efbc8fba1e0fa66dba6)" },
{ "_id": "$ObjectId(651ea60fc8fba1d0fa66dba7)" }
]
},
"options": {
"sort": {},
"projection": {}
},
"filter": {},
"payload": [],
"updateDoc": {
"$set": {}
},
"collection": "test",
"target": "result"
}
]
},
"connection": "{{$env.MONGODB_CONNECTION}}",
"database": "demo",
"description": "query collection"
}
]
},
"name": "exampleFindOrMongoDb",
"inputSchema": {},
"tags": ["example"],
"inputSchemaLabel": "",
"version": 1,
"id": "2b2afba2-2547-42fb-b703-9bb652e3f775",
"createDate": "2021-10-10T06:35:21.025Z"
}
{
"doc": {
"result": [
{
"_id": "651e9efbc8fba1e0fa66dba6",
"item": "cat"
},
{
"_id": "651ea60fc8fba1d0fa66dba7",
"item": "dog"
}
]
},
"errors": [],
"performance": {
"steps": [
{
"step": "mongo-db",
"executionTime": 57
}
],
"executionTimeOfFlow": 58,
"timeMetric": "ms"
}
}
Update
{
"stepType": "mongo-db",
"color": "rgb(95, 206, 165)",
"displayName": "MongoDB",
"isSelected": true,
"config": {
"actions": [
{
"action": "update",
"query": {},
"options": {
"sort": {},
"projection": {}
},
"filter": {
"title": "The New Document"
},
"payload": [
{
"title": "The New Document",
"body": "this is a short new document"
}
],
"updateDoc": {
"$set": {
"title": "Updated title"
}
},
"collection": "test",
"target": "result"
}
]
},
"connection": "{{$env.MONGODB_CONNECTION}}",
"database": "demo",
"description": "update documents into collection"
}
{
"doc": {
"result": {
"acknowledged": true,
"modifiedCount": 1,
"upsertedId": null,
"upsertedCount": 0,
"matchedCount": 1
}
},
"errors": [],
"performance": {
"steps": [
{
"step": "mongo-db",
"executionTime": 7
}
],
"executionTimeOfFlow": 7,
"timeMetric": "ms"
}
}
{
"name": "exampleMongodb",
"flow": {
"name": "exampleMongodb",
"description": "",
"steps": [
{
"stepType": "mongo-db",
"color": "rgb(95, 206, 165)",
"displayName": "MongoDB",
"isSelected": true,
"config": {
"actions": [
{
"action": "update",
"query": {},
"options": {
"sort": {},
"projection": {}
},
"filter": {
"title": "The New Document"
},
"payload": [
{
"title": "The New Document",
"body": "this is a short new document"
}
],
"updateDoc": {
"$set": {
"title": "Updated title"
}
},
"collection": "test",
"target": "result"
}
]
},
"connection": "{{$env.MONGODB_CONNECTION}}",
"database": "demo",
"description": "update documents into collection"
}
]
},
"inputSchema": {},
"createDate": "2021-10-10T07:31:09.258Z",
"id": 1633851069357
}
Aggregate
The query
must be array of objects when using this function.
To know more about aggregation stages, see MongoDb documentation
For below example the data in MongoDb collections is:
{
"id": "ef480376-f2c1-46a6-8c07-c869d672c17b",
"name": "mongoDbAggregateQueryExample",
"flow": {
"name": "mongoDbAggregateQueryExample",
"description": "",
"steps": [
{
"stepType": "mongo-db",
"color": "rgb(95, 206, 165)",
"displayName": "MongoDB",
"isSelected": true,
"config": {
"actions": [
{
"action": "aggregate",
"query": [
{
"$match": {
"org": "1"
}
},
{
"$group": {
"_id": "$dept",
"totalusers": {
"$sum": 1
}
}
}
],
"options": {
"sort": {},
"projection": {}
},
"filter": {},
"payload": [],
"updateDoc": {
"$set": {}
},
"collection": "users",
"target": "result"
}
]
},
"connection": "{{$env.MONGODB_CONNECTION}}",
"database": "testDb",
"textColor": "black"
}
]
},
"inputSchema": {},
"createDate": "2022-05-02T09:00:47.199Z",
"tags": ["general"],
"inputSchemaLabel": "",
"version": 1
}
{
"doc": {
"result": [
{
"_id": "4",
"totalusers": 2
},
{
"_id": "5",
"totalusers": 1
}
]
},
"errors": [],
"performance": {
"steps": [
{
"step": "mongo-db",
"executionTime": 20
}
],
"executionTimeOfFlow": 22,
"timeMetric": "ms"
}
}