MongoDB command
Last updated
Was this helpful?
Last updated
Was this helpful?
The MongoDB Command step allows you to execute any MongoDB command using the db.command
method.
With this step, you can leverage the full power of MongoDB commands to interact with your MongoDB database. You can perform advanced operations, run aggregation pipelines, execute complex queries, and take advantage of specialized features specific to MongoDB.
Make sure to use MongoDB Extended JSON (v2) in your queries to prevent issues with complex data types (for example, UUID or Date).
To run a MongoDB command, you simply need to select it in the action selection dropdown and provide the necessary parameters. UI Bakery will then execute the command using the db.command
method behind the scenes.
In the example below, the find
command retrieves documents from the users collection where the age
field is greater than or equal to 30. The projection
parameter specifies that only the name
and age
fields should be included in the result.
The Command Parameters field in this action step provides a familiar code editor interface allowing you to conveniently interact with the user interface and write JavaScript code, for example:
If the MongoDB Date property is of the BSON type, then in order to be able to filter by Date you should send not the date itself but rather an object with the key &date
inside the <, >, >e operators. Below is an example of the code you can use:
To perform data aggregation, you can use the aggregate
operation with the following parameters:
In this example, the aggregation pipeline consists of several stages:
$match
to filter documents based on the age
field
$group
to group documents by the status
field and calculate the count
You can also use the aggregate
command in MongoDB to perform complex aggregations on your data by combining multiple stages in a pipeline:
In this query, we're performing the following operations:
$match
- Filters the documents to include only those where the age
is greater than or equal to 30.
$lookup
- Joins the users collection with the orders collection based on the _id
field of users and the userId
field of orders. This creates an array called user_orders containing the matching orders for each user.
$unwind
- Deconstructs the user_orders array creating separate documents for each order associated with a user.
$group
- Groups the documents by _id
(which is the unique identifier of the user) and calculates the total number of orders (total_orders
) and the total amount of orders (total_amount
) by summing up the amount field from the user_orders array.
$sort
- Sorts the resulting documents in descending order based on the total_amount
field.
$limit
- Limits the result to a maximum of 10 documents.
This query performs a join and aggregation operation to find users aged 30 or above, counts their total number of orders, calculates the total amount of their orders, and sorts the results by the total order amount in descending order. Finally, it limits the result to the top 10 users with the highest total order amount.
To insert multiple documents into a collection, use the insertMany
command:
This example inserts two documents into the collection. Each document specifies the name
and age
fields.
The insert
command allows you to insert one or more documents into a collection. The ordered
option is set to false to continue inserting the remaining documents even if an error occurs for any individual document.
To update multiple documents in a collection, use the update
command:
This example updates all documents in the collection where the age
is greater or equal to 30. The $set
operator is used to set the status
field to inactive.
The update
command allows you to update one or more documents in a collection. The updates
array contains the update operations, where each operation has:
the q
field for the query/filter
the u
field for the update
the multi
field set to true to update multiple documents
To delete multiple documents from a collection, use the delete
command:
This example deletes all documents from the collection where the status
field is inactive
.
The delete
command allows you to delete one or more documents in a collection. The deletes
array contains the delete operations, where each operation has:
the q
field for the query/filter
the limit
field set to 0
to delete all matching documents
The findAndModify
command allows you to find a single document in your collection based on a filter condition and update it:
This operation will find a document in the "users" table with the name
"John" and update its age
to 40 and name
to John Doe.
The findAndModify
command is used to atomically find and modify a document in a collection:
the query
field specifies the filter for the document to find
the update
field specifies the replacement document
the new
option set to true
returns the modified document
UI Bakery supports MongoDB Extended JSON v2, so you can use the$oid
notation. For example, if you need to find one item by id, you can do it in the following way:
{find: 'orders', filter: { id: {$oid: 'required_id'}}}