DB::MONGO
Perform Mongo queries.
Prerequisite
Syntax
actions:
<action-id>:
type: DB::MONGO
props:
queryType: <query_type>
document: <document>
collection: <collection_name>
Properties
Property | Description | Type |
---|---|---|
<action-id> | Unique identifier for the action | string |
type | Type of the action | string |
props | Properties specific to this action. Refer to props section for more details. | object |
props
Property | Description | Type | Required |
---|---|---|---|
query | MongoDB query | object | true |
queryType | The type of MongoDB query. See the queryType section for more details | string | true |
document | The document to be inserted or updated in MongoDB | object | |
collection | The MongoDB collection in which to perform the query | string | true |
queryType
Property | Description |
---|---|
INSERT | Inserts a new document into the specified MongoDB collection |
SAVE | Updates an existing document in the specified MongoDB collection |
FIND | Retrieves documents from the specified MongoDB collection based on a query |
REMOVE_ALL | Deletes all documents from the specified MongoDB collection |
Output
📝 Note: Can be accessed using
context["last_output"]
Key | Description | Type |
---|---|---|
result | Outcome of the query, either an _id , a removed_count , or a list of documents. Refer to result section. | object or array |
timeTaken | Time taken, in milliseconds | long |
error | Error message, if any, produced while executing the query | string |
result
Property | Description | Type |
---|---|---|
INSERT | Returns the _id of the inserted document | object |
SAVE | Returns the _id of the updated document or null on failure | object |
FIND | Returns a list of documents found | array |
REMOVE_ALL | Returns the number of removed documents as removed_count | object |
{ "result": { "_id": "63c1fe90f1582d72983caf6f" } }
{ "result": { "_id": "63c1fe90f1582d72983caf6f" } }
{
"result": [
{ "_id": "63c1fe90f1582d72983caf6f", "name": "John" },
{ "_id": "94c1fe90f1582d72983caf6c", "name": "Sarah" }
]
}
{ "result": { "removed_count": 1 } }
Example
mongoDbTests.yml
actions:
insert:
type: DB::MONGO
props:
queryType: INSERT
document: { "name": "John" }
collection: users
save:
type: DB::MONGO
props:
query: { "_id": "63c1fe90f1582d72983caf6f", "name": "John Jr" }
queryType: SAVE
collection: users
find:
type: DB::MONGO
props:
query: { "name": "John Jr" }
queryType: FIND
collection: users
remove:
type: DB::MONGO
props:
query: { "name": "John" }
queryType: REMOVE_ALL
collection: users
tests:
mongodbTest:
tasks:
- resource: resources.mongodb
action: actions.insert
- resource: resources.mongodb
action: actions.save
- resource: resources.mongodb
action: actions.find
- resource: resources.mongodb
action: actions.remove