Skip to main content
Version: ACE 4

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 to
  • Configuration - list of queries to be made
    • action - the action to preform - currently supported are insert, find, update, delete and aggregate methods
    • collection name - the name of the collection on which to execute the query
    • target path - the doc node on which to attach the result of query
    • payload - used for insert query to define payload to be inserted into collection
    • query - the query to preform on collection (used for find, delete and aggregate)
    • options - used for find method - additional sorting options for query
    • filter - used for update method - the filter used to determine which documents to update
    • updated item - the update payload
note

Find syntax differs from Mongo shell, see Node.js driver documentation

note

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

MongoDb insert step configuration
{
"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"
}
Result
{
"doc": {
"result": {
"acknowledged": true,
"insertedCount": 1,
"insertedIds": {
"0": "62bc695ecba369afc6709388"
}
}
},
"errors": [],
"performance": {
"steps": [
{
"step": "mongo-db",
"executionTime": 61
}
],
"executionTimeOfFlow": 61,
"timeMetric": "ms"
}
}

Find

note

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>).

MongoDb find step configuration
{
"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": ""
}
Result
{
"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

Mongo Db find by id flow example
{
"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"
}
Result
{
"doc": {
"result": [
{
"_id": "651e9efbc8fba1d0fa66dba6",
"item": "something"
}
]
},
"errors": [],
"performance": {
"steps": [
{
"step": "mongo-db",
"executionTime": 57
}
],
"executionTimeOfFlow": 58,
"timeMetric": "ms"
}
}

Find with $or

Mongo Db find $or flow example
{
"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"
}
Result
{
"doc": {
"result": [
{
"_id": "651e9efbc8fba1e0fa66dba6",
"item": "cat"
},
{
"_id": "651ea60fc8fba1d0fa66dba7",
"item": "dog"
}
]
},
"errors": [],
"performance": {
"steps": [
{
"step": "mongo-db",
"executionTime": 57
}
],
"executionTimeOfFlow": 58,
"timeMetric": "ms"
}
}

Update

Step configuration
{
"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"
}
Result
{
"doc": {
"result": {
"acknowledged": true,
"modifiedCount": 1,
"upsertedId": null,
"upsertedCount": 0,
"matchedCount": 1
}
},
"errors": [],
"performance": {
"steps": [
{
"step": "mongo-db",
"executionTime": 7
}
],
"executionTimeOfFlow": 7,
"timeMetric": "ms"
}
}
MongoDb update flow example
{
"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:

Flow example
{
"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
}
Result
{
"doc": {
"result": [
{
"_id": "4",
"totalusers": 2
},
{
"_id": "5",
"totalusers": 1
}
]
},
"errors": [],
"performance": {
"steps": [
{
"step": "mongo-db",
"executionTime": 20
}
],
"executionTimeOfFlow": 22,
"timeMetric": "ms"
}
}