MongoDB command

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)

MongoDB command example

To run a MongoDB command, provide the necessary parameters. UI Bakery will execute the command using the db.command method behind the scenes.

{
  find: "users",
  filter: { age: { $gte: 30 } },
  projection: { name: 1, age: 1 }
}

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.:

const minAge = +{{ui.input.value}}

return {
  find: "users",
  filter: { age: { $gte: minAge } },
  projection: { name: 1, age: 1 }
}

UI Bakery will execute this command using the db.command method.

Sample: Aggregate

To perform data aggregation, you can use the aggregate operation with following parameter:

{
  aggregate: "users",
  pipeline: [
    { $match: { age: { $gte: 30 } } },
    { $group: { _id: "$status", count: { $sum: 1 } } }
  ],
  cursor: { batchSize: 10 }
}

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.

Sample: Insert Multiple Documents

To insert multiple documents into a collection, use the insertMany command:

{
  insert: "users",
  documents: [
    { name: "John", age: 30 },
    { name: "Jane", age: 25 }
  ],
  ordered: false
}

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.

Sample: Update Multiple Documents

To update multiple documents in a collection, use the update command:

{
  update: "users",
  updates: [
    { q: { age: { $gte: 30 } }, u: { $set: { status: "inactive" } }, multi: true }
  ]
}

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.

Sample: Delete Multiple Documents

To delete multiple documents from a collection, use the delete command:

{
  delete: "users",
  deletes: [
    { q: { status: "inactive" }, limit: 0 }
  ]
}

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.

Sample: Find One and Modify

The findAndModify command allows you to find a single document in your collection based on a filter condition and update it.

{
  findAndModify: "users",
  query: { name: "John" },
  update: { $set: { name: "John Doe", age: 40 } },
  new: true
}

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'}}}

Sample: Join and Aggregate

The aggregate command in MongoDB allows you to perform complex aggregations on your data by combining multiple stages in a pipeline.

{
  aggregate: "users",
  pipeline: [
    { $match: { age: { $gte: 30 } } },
    { $lookup: { from: "orders", localField: "_id", foreignField: "userId", as: "user_orders" } },
    { $unwind: "$user_orders" },
    { $group: { _id: "$_id", total_orders: { $sum: 1 }, total_amount: { $sum: "$user_orders.amount" } } },
    { $sort: { total_amount: -1 } },
    { $limit: 10 }
  ],
  cursor: { batchSize: 10 }
}

In this query, we're performing the following operations:

  1. $match: Filters the documents to include only those where the age is greater than or equal to 30.

  2. $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.

  3. $unwind: Deconstructs the "user_orders" array, creating separate documents for each order associated with a user.

  4. $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.

  5. $sort: Sorts the resulting documents in descending order based on the total_amount field.

  6. $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.

Last updated