Skip to main content
Version: ACE 5

MongoDb

Overview​

MongoDb step is used to access MongoDb and make basic queries on the database (inserting, selecting, updating and deleting)

Configuration​

  • Connection String (connection) - 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 (database) - the MongoDB database to connect to
    • Actions (actions) - list of queries to be made
      • Action (actions[].action) - action to perform: insert, find, update, delete, aggregate
      • Collection Name (actions[].collection) - name of the collection on which to execute the query
      • Target path (actions[].target) - the doc node on which to attach the result of query
      • Query (actions[].query) - query to perform on collection
      • Options (actions[].options) - additional options for query
      • Payload (actions[].payload) - used for insert query to define payload to be inserted into collection
      • Filter (actions[].filter) - filter used to determine which documents to update
      • UpdateDoc (actions[].updateDoc) - update payload
note

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

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 flow example
tags: []
steps:
- stepType: mongo-db
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: "mongodb://mongo:27017"
database: demo
description: insert documents into collection
name: MongoDB
condition: ""
sampleData: {}
Flow 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 example
tags:
- example
steps:
- stepType: mongo-db
config:
actions:
- action: find
query: {}
options:
sort: {}
projection: {}
filter: {}
payload: []
updateDoc:
$set: {}
collection: test
target: query
connection: "mongodb://mongo:27017"
database: demo
description: query collection
name: MongoDB
condition: ""
sampleData: {}
Result
{
"doc": {
"query": [
{
"_id": "64df4a17d3527170778b07f0",
"title": "The New Document",
"body": "this is a short new document"
}
]
},
"errors": [],
"performance": {
"steps": [
{
"step": "mongo-db",
"executionTime": 13
}
],
"executionTimeOfFlow": 13,
"timeMetric": "ms"
}
}

Find by id​

MongoDb find by id example
tags:
- example
steps:
- stepType: mongo-db
config:
actions:
- action: find
query:
_id: $ObjectId(64df4a17d3527170778b07f0)
options:
sort: {}
projection: {}
filter: {}
payload: []
updateDoc:
$set: {}
collection: test
target: result
connection: "mongodb://mongo:27017"
database: demo
description: query collection
name: MongoDB
condition: ""
sampleData: {}
Result
{
"doc": {
"result": [
{
"_id": "64df4a17d3527170778b07f0",
"item": "something"
}
]
},
"errors": [],
"performance": {
"steps": [
{
"step": "mongo-db",
"executionTime": 57
}
],
"executionTimeOfFlow": 58,
"timeMetric": "ms"
}
}

Find with $or​

MongoDb find $or example
tags:
- example
steps:
- stepType: mongo-db
config:
actions:
- action: find
query:
$or:
- _id: $ObjectId(651e9efbc8fba1e0fa66dba6)
- _id: $ObjectId(651ea60fc8fba1d0fa66dba7)
options:
sort: {}
projection: {}
filter: {}
payload: []
updateDoc:
$set: {}
collection: test
target: result
connection: "mongodb://mongo:27017"
database: demo
description: query collection
name: MongoDB
condition: ""
sampleData: {}
Result
{
"doc": {
"result": [
{
"_id": "651e9efbc8fba1e0fa66dba6",
"item": "cat"
},
{
"_id": "651ea60fc8fba1d0fa66dba7",
"item": "dog"
}
]
},
"errors": [],
"performance": {
"steps": [
{
"step": "mongo-db",
"executionTime": 57
}
],
"executionTimeOfFlow": 58,
"timeMetric": "ms"
}
}

Update​

MongoDb update example
tags: []
steps:
- stepType: mongo-db
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: "mongodb://mongo:27017"
database: demo
description: update documents into collection
name: MongoDB
condition: ""
sampleData: {}
Result
{
"doc": {
"result": {
"acknowledged": true,
"modifiedCount": 1,
"upsertedId": null,
"upsertedCount": 0,
"matchedCount": 1
}
},
"errors": [],
"performance": {
"steps": [
{
"step": "mongo-db",
"executionTime": 11
}
],
"executionTimeOfFlow": 11,
"timeMetric": "ms"
}
}

Aggregate​

The query must be an 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:

In example below there is 2 Mongo steps - to set up data, and to aggregate data.

Aggregate flow example
tags:
- general
sampleInputSchema: ""
sampleData: {}
description: ""
steps:
- name: Prepare data
description: ""
config:
actions:
- action: delete
query: {}
options:
sort: {}
projection: {}
collection: users
target: deleteResult
- action: insert
payload:
- name: User 1
dept: "4"
designation: student
org: "1"
- name: User 2
dept: "4"
designation: teacher
org: "1"
- name: User 3
dept: "5"
designation: teacher
org: "1"
- name: User 4
dept: "5"
designation: teacher
org: "2"
query: {}
options:
sort: {}
projection: {}
collection: users
target: insertResult
stepType: mongo-db
connection: mongodb://mongo:27017
database: demo
condition: ""
- name: MongoDB aggregate
description: ""
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
stepType: mongo-db
connection: mongodb://mongo:27017
database: demo
condition: ""
Result
{
"doc": {
"deleteResult": {
"acknowledged": true,
"deletedCount": 4
},
"insertResult": {
"acknowledged": true,
"insertedCount": 4,
"insertedIds": {
"0": "64df4e44d3527170778b0803",
"1": "64df4e44d3527170778b0804",
"2": "64df4e44d3527170778b0805",
"3": "64df4e44d3527170778b0806"
}
},
"result": [
{
"_id": "4",
"totalusers": 2
},
{
"_id": "5",
"totalusers": 1
}
]
},
"errors": [],
"performance": {
"steps": [
{
"step": "mongo-db",
"executionTime": 12
},
{
"step": "mongo-db",
"executionTime": 10
}
],
"executionTimeOfFlow": 22,
"timeMetric": "ms"
}
}