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
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
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: {}
{
"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>)
.
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: {}
{
"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
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: {}
{
"doc": {
"result": [
{
"_id": "64df4a17d3527170778b07f0",
"item": "something"
}
]
},
"errors": [],
"performance": {
"steps": [
{
"step": "mongo-db",
"executionTime": 57
}
],
"executionTimeOfFlow": 58,
"timeMetric": "ms"
}
}
Find with $or
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: {}
{
"doc": {
"result": [
{
"_id": "651e9efbc8fba1e0fa66dba6",
"item": "cat"
},
{
"_id": "651ea60fc8fba1d0fa66dba7",
"item": "dog"
}
]
},
"errors": [],
"performance": {
"steps": [
{
"step": "mongo-db",
"executionTime": 57
}
],
"executionTimeOfFlow": 58,
"timeMetric": "ms"
}
}
Update
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: {}
{
"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.
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: ""
{
"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"
}
}