menu

Search By Label

To query a MongoDB collection to search a RegExp, you can use the following syntax in a MongoDB query:
db.collection.find({
    name: {$regex: "Match here"}
})
To query a MongoDB collection where a specific field both exists and is not empty, you can use the following syntax in a MongoDB query (using the find method):
db.collection.find({
  description: { $exists: true, $ne: "" }
})

$exists: true: ensures the field exists.
$ne: "": ensures the field is not an empty string.

The aggregation frameworks is a pipeline of steps that runs on your data to convert the output on the format you need it. It provides a set of stages that you can chain together to create pipelines that process your data in a series of steps.

Here are some of the common aggregation stages:
  • $match: Filters documents based on specified criteria.
  • $project: Selects or excludes fields from documents.
  • $group: Groups documents by a specified field and calculates aggregate values.
  • $sort: Sorts documents by a specified field.
  • $limit: Limits the number of documents returned.
  • $skip: Skips a specified number of documents.
  • $unwind: Unwinds an array field, creating a new document for each element in the array.
  • $lookup: Joins two collections based on a specified field.
  • $redact: Redacts fields in documents based on specified criteria.
  • $bucket: Buckets documents into groups based on a specified field.
  • $sample: Samples a random subset of documents.
  • $geoNear: Finds documents near a specified point.


Example: Calculating Average Order Value
Scenario: Let's assume we have a collection named orders with documents representing individual orders. Each document has fields like order_id, customer_id, product_name, and price. We want to calculate the average order value for each customer.
db.orders.aggregate([
  {
    $group: {
      _id: "$customer_id",
      total_spent: { $sum: "$price" },
      total_orders: { $count: {} }
    }
  },
  {
    $project: {
      average_order_value: { $divide: ["$total_spent", "$total_orders"] }
    }
  }
])

Explanation:
  1. $group:
    • Groups the documents by customer_id.
    • Calculates the total spent for each customer using $sum.
    • Counts the total number of orders for each customer using $count.
  2. $project:
    • Calculates the average order value by dividing total_spent by total_orders.

Result:
The aggregation pipeline will return a list of documents, each containing the customer_id and the calculated average_order_value.

When you open a new connection for `mongosh` from your terminal you will find really useful the following commands:

First of all, connect to a server:
mongosh "mongodb+srv://{MONGO_COLLECTION_USER}:{MONGO_COLLECTION_PASSWORD}@{MONGO_APP_NAME}.yng1j.mongodb.net/?appName={MONGO_APP_NAME}"

Then the basic commands to start interacting (the most of them are little obvious):
// show databases
show dbs
// use database
use <db_name>
// show collections
show collections
// finally interact with them, for example
db.users.findOne()
Time to Live (TTL) is a feature in MongoDB that allows you to automatically expire documents after a specified amount of time. This is useful for scenarios where you want to keep data for a limited duration, such as temporary data, session data, or cache entries.

How TTL works:
  1. Index creation: To enable TTL for a collection, you create an index on a field that represents the expiration time. This field must be of type Date.
  2. Expiration time setting: When creating the index, you specify the TTL value in seconds.
  3. Document expiration: MongoDB periodically scans the collection and deletes documents whose expiration time has passed.

Example:
db.sessions.createIndex({ expiresAt: 1 }, { expireAfterSeconds: 3600 });
This creates an index on the expiresAt field and sets the TTL to 1 hour (3600 seconds). Any documents in the sessions collection with an expiresAt value that is older than 1 hour will be automatically deleted.

Use cases for TTL:
  • Session management: Store session data with a TTL to automatically expire inactive sessions.
  • Temporary data: Keep temporary data for a limited time, such as cached results or temporary files.
  • Data retention policies: Implement data retention policies by setting appropriate TTL values for different types of data.
The explain("executionStats") command in MongoDB provides detailed information about the execution plan and performance metrics of a query.
When used with the find() method, it returns a document containing statistics about how MongoDB executed the query.

Example:
db.products.explain("executionStats").find({ price: { $gt: 10 } });
This aggregation stage performs a left outer join to a collection in the same database.

There are four required fields:
  • from: The collection to use for lookup in the same database
  • localField: The field in the primary collection that can be used as a unique identifier in the from collection.
  • foreignField: The field in the from collection that can be used as a unique identifier in the primary collection.
  • as: The name of the new field that will contain the matching documents from the from collection.

Example:
db.comments.aggregate([
  {
    $lookup: {
      from: "movies",
      localField: "movie_id",
      foreignField: "_id",
      as: "movie_details",
    },
  },
  {
    $limit: 1
  }
])