MongoDB command
Last updated
Last updated
© 2024 UI Bakery
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. 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 (e.g. UUID or Date)
To run a MongoDB command, provide the necessary parameters. UI Bakery will execute the command using the db.command
method behind the scenes.
In this example, 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 provides a familiar code editor interface, allowing you to conveniently interact with the user interface and write JavaScript code, e.g.:
UI Bakery will execute this command using the db.command
method.
To perform data aggregation, you can use the aggregate
operation with following parameter:
In this example, the aggregation pipeline consists of multiple stages: $match
to filter documents based on the age
field, $group
to group documents by the status
field and calculate the count
.
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, and 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 and 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, and the new
option set to true
returns the modified document.
UI Bakery supports MongoDB Extended JSON v2, so you can use$oid
notation.
If you need to find one item by id, you can do it this way:
{find: 'orders', filter: { id: {$oid: 'required_id'}}}
The aggregate
command in MongoDB allows you 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 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.