MongoDB component Action functions
Related pages:
MongoDB
MongoDB Technical Notes
Description
Here are the details of the action function of MongoDB component.
Aggregate
Calculates aggregate values for the data in a collection or a view
Configuration
- Select a DB - Database to be found in.
- Select a Collection - Collection to be found in.
- Select an Emit Behavior: Emit Individually, Emit Batch or Fetch All - Determines how many messages to emit when the number of results is not one. Emit Individually attaches a
groupInfo
to the message (outside the body) which allows the message to be rebuilt. - allowDiskUse - Enables writing to temporary files. When set to true, aggregation stages can write data to the _tmp subdirectory in the dbPath directory. See docs.
Body
pipeline
- a sequence of data aggregation operations or stages. See the
aggregation pipeline operators for details. Required field.
Note: Must be an array of pipeline stages.
batchSize
- size of pages. Rendered only if Emit Batch
strategy is chosen. Default: 10.
Examples
{
"pipeline": [
{ $group: { _id: null, count: { $sum: 1 } } }
]
}
{
"pipeline": [
{ $match: { value: 64 } },
{ $addFields: { newField: 88 } },
]
}
Emit batch strategy
{
"pipeline": [
{ $match: { value: 64 } },
{ $addFields: { newField: 88 } },
],
"batchSize": 10
}
Bulk Write
Bulk Write takes an array of write operations and executes each of them. Operations executed in provided order.
Configuration
- Select a DB - Database to perform operations.
- Select a Collection - collection to perform operations.
Body
operations
- Valid operations are: insertOne
, updateOne
, updateMany
, deleteOne
, deleteMany
, replaceOne
Example
{
"operations": [
{
"insertOne": {
"document": {
"test": 1
}
}
},
{
"updateOne": {
"filter": {
"test": "test"
},
"update": {
"test": 2
}
}
}
]
}
Delete By ID
Delete document by ID.
Configuration
- Select a DB - Database to be found in.
- Select a Collection - Collection to be found in.
Body
‘id’ of the document to delete
Example
{
"id" : "5e936f7c7c876ec2e1e48f4d"
}
Result
Resulting JSON is a MongoDB specific information
{
"result": {
"result": {
"n": 1,
"opTime": {
"ts": "6815603303313833985",
"t": 24
},
"electionId": "7fffffff0000000000000018",
"ok": 1,
"$clusterTime": {
"clusterTime": "6815603303313833985",
"signature": {
"hash": "8NaPN38tce9du4OTS1aMF9qZeoI=",
"keyId": "6781502667437899778"
}
},
"operationTime": "6815603303313833985"
},
"connection": {
"_events": {},
"_eventsCount": 4,
"id": 1,
"address": "35.195.12.99:27017",
"bson": {},
"socketTimeout": 360000,
"monitorCommands": false,
"closed": false,
"destroyed": false,
"lastIsMasterMS": 10
},
"deletedCount": 1,
"n": 1,
"opTime": {
"ts": "6815603303313833985",
"t": 24
},
"electionId": "7fffffff0000000000000018",
"ok": 1,
"$clusterTime": {
"clusterTime": "6815603303313833985",
"signature": {
"hash": "8NaPN38tce9du4OTS1aMF9qZeoI=",
"keyId": "6781502667437899778"
}
},
"operationTime": "6815603303313833985"
}
}
Delete By Unique Criteria
Delete document by Unique Criteria.
Configuration
- Select a DB - Database to be found in.
- Select a Collection - Collection to be found in.
Body
criteria
of the document to search
Example
{
"criteria": {
"foo" : "bar"
}
}
Result
Resulting JSON is a MongoDB specific information
{
"result": {
"result": {
"n": 2,
"opTime": {
"ts": "6815625881956909058",
"t": 24
},
"electionId": "7fffffff0000000000000018",
"ok": 1,
"$clusterTime": {
"clusterTime": "6815625881956909058",
"signature": {
"hash": "PfOoj8NNKraOSMQIqufBVgFis9g=",
"keyId": "6781502667437899778"
}
},
"operationTime": "6815625881956909058"
},
"connection": {
"_events": {},
"_eventsCount": 4,
"id": 1,
"address": "35.195.12.99:27017",
"bson": {},
"socketTimeout": 360000,
"monitorCommands": false,
"closed": false,
"destroyed": false,
"lastIsMasterMS": 10
},
"deletedCount": 2,
"n": 2,
"opTime": {
"ts": "6815625881956909058",
"t": 24
},
"electionId": "7fffffff0000000000000018",
"ok": 1,
"$clusterTime": {
"clusterTime": "6815625881956909058",
"signature": {
"hash": "PfOoj8NNKraOSMQIqufBVgFis9g=",
"keyId": "6781502667437899778"
}
},
"operationTime": "6815625881956909058"
}
}
Lookup By ID
Lookup document by ID.
Configuration
- Select a DB - Database to be found in.
- Select a Collection - Collection to be found in.
Body
id
of the document
Example
{
"id" : "5e936f7c7c876ec2e1e48f4d"
}
Result
{
"result": {
"_id": "5a97f9c91c807bb9c6eb5fb4",
"user_id": "t3qulfeem@kwiv5.6ur",
"name": "John Smith"
}
}
Lookup By Unique Criteria
Lookup (at most 1) Document By Unique Criteria.
Configuration
- Select a DB - Database to be found in.
- Select a Collection - Collection to be found in.
- Allow zero results - When checked, in case zero documents found, an empty body will be emitted as a result. An error will be thrown otherwise.
Action logic:
- In case 0 document found:
- ‘Allow zero results’ checked. Empty object is emitted.
- ‘Allow zero results’ not checked. Error ‘Document not found’ thrown.
- In case exactly 1 document found:
This document is emitted
- In case more than 1 document found: Error ‘More than one document found’ thrown.
Body
‘criteria’ of the document to search. If you provide ObjectId
it will be automatically parsed to MongoDB ObjectId
Examples
{
"criteria": {
"value" : 4
}
}
{
"criteria": {
"_id" : "ObjectId('64a296786751183ae1f615ed')"
}
}
Result
{
"_id": "5a97f9c91c807bb9c6eb5fb4",
"user_id": "t3qulfeem@kwiv5.6ur",
"name": "John Smith",
"value": 4
}
Lookup Plural
Lookup many documents by criteria.
Configuration
- Select a DB - Database to be found in.
- Select a Collection - Collection to be found in.
- Select an Emit Behavior: Emit Individually, Emit Batch or Fetch All - Determines how many messages to emit when the number of results is not one. Emit Individually attaches a
groupInfo
to the message (outside the body) which allows the message to be rebuilt.
Body
‘criteria’ of the document to search. Required field. If you provide ObjectId
it will be automatically parsed to MongoDB ObjectId
Please Note:: it is possible to lookup objects by fields with type ObjectID. For enabling this feature use template
"ObjectId('objectId')"
.
Example
{
"criteria": {
"taskId": "ObjectId('61523b6043a05f0006fc4800')"
},
"limit": 100
}
Known limitation: it is possible to lookup by ObjectId only on first level of criteria object. Nested properties are not supported. In case usage template
"ObjectId('objectId')"
on next levels of criteria object value will accept as string.
limit
- specifies the maximum number of documents the action will return. 0 is equivalent to setting no limit. Optional field.
project
- specifies which fields, including embedded objects, the action should return. Please refer Project Fields to Return from Query for the details. Optional field.
batchSize
- size of pages. Rendered only if Emit Batch
strategy is chosen. Default: 10.
Example
This will find all the documents (plural) according to th given criteria. In this case this will retrieve all the objects with the root value equals 4:
{
"criteria": {
"value": 4
},
"limit": 100
}
Result
Will return an array of documents inside the result
property:
{
"result": [
{
"_id": "5e936f7c7c876ec2e1e48f4d",
"name": "Example4",
"nestedObj": {
"otherValue": 4
},
"value": 4
}
]
}
- Complex queries are also supported. E.g. the next query will return all the objects that have a property
otherValue
equals 4 inside of a propertynestedObj
. You will get a ‘Projection cannot have a mix of inclusion and exclusion’ error otherwise.
{
"criteria": {
"nestedObj.otherValue": 4
}
}
- Using project object you can specify which fields to return (set to 1) or not to return (set to 0). The following sample returns all the objects (as the ‘criteria’ object is empty), with fields ‘fieldFoo’ and ‘fieldBar’ only. Also note that with the exception of the _id field, you cannot combine inclusion and exclusion statements in projection documents.
{
"criteria": {},
"project": { 'fieldFoo': 1, 'fieldBar': 1, '_id': 0}
}
- Emit batch strategy:
{
"criteria": {
"value": 4
},
"limit": 100,
"batchSize": 10
}
Upsert By ID
Upserts document by ID.
Configuration
- Select a DB - Database to be found in.
- Select a Collection - Collection to be found in.
- Object ID - id of upserted document. If not provided document would be created. Note: value must be a single String of 12 bytes or a string of 24 hex characters.
Body
Document to be upserted.
Example
{
"name" : "some_value",
"surname": "updated_value"
}
Result
{
"result": {
"id": "some id" // object id
// other document properties
}
}
Update Many
Updates documents in a collection
Configuration
- Select a DB - Database to be found in.
- Select a Collection - Collection to be found in.
- Upsert - When checked, creates a new document if no document matches the query.
Body
criteria
- the criteria used to select the documents to update. If you provideObjectId
it will be automatically parsed to MongoDBObjectId
update
- the update operations to be applied to the documents
Please refer the MongoDB Update Operators Documentation for the details on the operators.
Examples
{
"criteria": {
"nestedObj.otherValue": {
"$gt": 2
}
},
"update": {
"$set": {
"nestedObj.anotherValue":"bla"
}
}
}
{
"criteria": {
"_id" : "ObjectId('64a296786751183ae1f615ed')"
},
"update": {
"$set": {
"nestedObj.anotherValue":"bla"
}
}
}
Result
Resulting JSON
{
"result": {
"result": {
"n": 2,
"nModified": 2,
"opTime": {
"ts": "6816718766450147330",
"t": 24
},
"electionId": "7fffffff0000000000000018",
"ok": 1,
"$clusterTime": {
"clusterTime": "6816718766450147330",
"signature": {
"hash": "5wbixQ6vmIt6IJehzdL55zp1zYw=",
"keyId": "6781502667437899778"
}
},
"operationTime": "6816718766450147330"
},
"connection": {
"_events": {},
"_eventsCount": 4,
"id": 1,
"address": "35.195.12.99:27017",
"bson": {},
"socketTimeout": 360000,
"monitorCommands": false,
"closed": false,
"destroyed": false,
"lastIsMasterMS": 10
},
"modifiedCount": 2,
"upsertedId": null,
"upsertedCount": 0,
"matchedCount": 2,
"n": 2,
"nModified": 2,
"opTime": {
"ts": "6816718766450147330",
"t": 24
},
"electionId": "7fffffff0000000000000018",
"ok": 1,
"$clusterTime": {
"clusterTime": "6816718766450147330",
"signature": {
"hash": "5wbixQ6vmIt6IJehzdL55zp1zYw=",
"keyId": "6781502667437899778"
}
},
"operationTime": "6816718766450147330"
}
}
Upsert By Unique Criteria
Upserts document by unique criteria.
Configuration
- Select a DB - Database to be found in.
- Select a Collection - Collection to be found in.
Body
criteria
- criteria for upsertion. If you provideObjectId
it will be automatically parsed to MongoDBObjectId
Examples
{
"address": "Park Lane 38"
} // Property address equals 'Park Lane 38' in upserted Document
{
"status": {
"$in": "['A', 'D']"
}
} // Property status equals 'A' or 'D'
{
"_id" : "ObjectId('64a296786751183ae1f615ed')"
} // Document MongoDB id equal '64a296786751183ae1f615ed'
value
- Document to be upserted.
Example
{
"name" : "some_value",
"surname": "updated_value"
}
Result
{
"result": {
"id": "some id" // object id
// other document properties
}
}