Skip to content
Documentation
Operation Keys (OPKs)

Operation Keys (OPKs)

In VERSE.DB we have 3 types of the Operation Keys (OPKs) and here you can find them all for all your needs

Query Operation Keys (QOK)

Revised Example JSON Data:

We'll use a sample data structure for the database to demonstrate the queries.

[
  {
    "id": 1,
    "username": "john_doe",
    "age": 25,
    "email": "john@example.com",
    "tags": ["developer", "male"],
    "profile": {
      "bio": "Software developer",
      "interests": ["coding", "reading"],
      "verified": true
    },
    "created_at": "2021-01-01T00:00:00Z"
  },
  {
    "id": 2,
    "username": "jane_doe",
    "age": 30,
    "email": "jane@example.com",
    "tags": ["designer", "female"],
    "profile": {
      "bio": "Graphic designer",
      "interests": ["art", "travel"],
      "verified": false
    },
    "created_at": "2021-02-01T00:00:00Z"
  }
]

Usage for Each Operation:

  1. Equality ($eq):

    const query = { age: 25 };
    const result = await db.find("users", query);
    • Finds users whose age is exactly 25.
  2. Regex ($regex):

    const query = { username: { $regex: "^john" } };
    const result = await db.find("users", query);
    • Finds users whose username starts with "john".
  3. Exists ($exists):

    const query = { "profile.bio": { $exists: true } };
    const result = await db.find("users", query);
    • Finds users who have a bio in their profile.
  4. Greater Than ($gt):

    const query = { age: { $gt: 25 } };
    const result = await db.find("users", query);
    • Finds users older than 25.
  5. Less Than ($lt):

    const query = { age: { $lt: 30 } };
    const result = await db.find("users", query);
    • Finds users younger than 30.
  6. In ($in):

    const query = { tags: { $in: ["developer"] } };
    const result = await db.find("users", query);
    • Finds users who have "developer" as one of their tags.
  7. Not Equal ($ne):

    const query = { username: { $ne: "john_doe" } };
    const result = await db.find("users", query);
    • Finds users whose username is not "john_doe".
  8. Element Match ($elemMatch):

    const query = { "profile.interests": { $elemMatch: { $eq: "coding" } } };
    const result = await db.find("users", query);
    • Finds users who have "coding" as one of their interests in the profile.
  9. Type Of ($typeOf):

    const query = { "profile.verified": { $typeOf: "boolean" } };
    const result = await db.find("users", query);
    • Finds users whose profile verification status is a boolean.
  10. And ($and):

    const query = { $and: [{ age: { $gt: 20 } }, { age: { $lt: 30 } }] };
    const result = await db.find("users", query);
    • Finds users older than 20 and younger than 30.
  11. Validate ($validate):

    const query = {
      age: { $validate: (value: number) => value > 18 && value < 40 },
    };
    const result = await db.find("users", query);
    • Finds users whose age is between 18 and 40 using a custom validation function.
  12. Or ($or):

    const query = { $or: [{ age: { $lt: 25 } }, { age: { $gt: 30 } }] };
    const result = await db.find("users", query);
    • Finds users younger than 25 or older than 30.
  13. Size ($size):

    const query = { tags: { $size: 2 } };
    const result = await db.find("users", query);
    • Finds users who have exactly 2 tags.
  14. Not In ($nin):

    const query = { username: { $nin: ["john_doe", "jane_doe"] } };
    const result = await db.find("users", query);
    • Finds users whose username is neither "john_doe" nor "jane_doe".
  15. Slice ($slice):

    const query = { "profile.interests": { $slice: 1 } };
    const result = await db.find("users", query);
    • Finds users and slices their interests array to only include the first element.
  16. Sort ($sort):

    const query = { "profile.interests": { $sort: 1 } };
    const result = await db.find("users", query);
    • Finds users and sorts their interests array in ascending order.
  17. Text ($text):

    const query = { "profile.bio": { $text: "developer" } };
    const result = await db.find("users", query);
    • Finds users whose bio includes the word "developer".
  • Equality ($eq): Finds users with a specific value.
  • Regex ($regex): Finds users whose string fields match a regular expression.
  • Exists ($exists): Finds users with a specific field.
  • Greater Than ($gt): Finds users with field values greater than a given value.
  • Less Than ($lt): Finds users with field values less than a given value.
  • In ($in): Finds users whose field values are in a given list.
  • Not Equal ($ne): Finds users whose field values are not equal to a given value.
  • Element Match ($elemMatch): Finds users with array fields that match a given condition.
  • Type Of ($typeOf): Finds users whose field values are of a specific type.
  • And ($and): Finds users who match all conditions in a given list.
  • Validate ($validate): Finds users whose field values match a custom validation function.
  • Or ($or): Finds users who match at least one condition in a given list.
  • Size ($size): Finds users with array fields of a specific size.
  • Not In ($nin): Finds users whose field values are not in a given list.
  • Slice ($slice): Limits array fields to a specific number of elements.
  • Sort ($sort): Sorts array fields in ascending or descending order.
  • Text ($text): Finds users whose string fields contain a given text.

Update Operation Keys (UPK)

Revised Example JSON Data:

We'll use a sample data structure for the database to demonstrate the queries.

[
  "person": {
    "name": {
      "first": "Alice",
      "last": "Smith"
    },
    "age": 30,
    "hobbies": ["reading", "swimming"],
    "addresses": [
      {
        "city": "New York",
        "country": "USA"
      },
      {
        "city": "Los Angeles",
        "country": "USA"
      }
    ]
  }
]

Usage for Each Operation:

  1. $set: Sets the value of a field in the document.

    const updateQuery = {
        $set: {
            "person.name.first": "Bob",
             { person: { name: 35 } }
        }
    };

    Explanation: This will set the name.first field to "Bob" and the age field to 35 in the person object.

  2. $unset: Removes the specified field(s) from the document.

    const updateQuery = {
      $unset: {
        "person.addresses[0].city": true,
      },
    };

    Explanation: This will remove the city field from the first index in person.addresses object.

  3. $push: Appends a value to an array field in the document.

    const updateQuery = {
      $push: {
        "person.hobbies": "painting",
      },
    };

    Explanation: This will add "painting" to the hobbies array field in the person object.

    • Note: you can use $each and $all in push to act as $pushAll/$pushMany.
  4. $pull: Removes all occurrences of a specified value or values from an array field.

    const updateQuery = {
      $pull: {
        "person.hobbies": "swimming",
      },
    };

    Explanation: This will remove all occurrences of "swimming" from the hobbies array field in the person object.

  • Note: you can use $each and $all in push to act as $pullAll/$pullMany.
  1. $addToSet: Adds elements to an array field only if they are not already present.

    const updateQuery = {
      $addToSet: {
        "person.hobbies": { $each: ["painting", "cooking"] },
      },
    };

    Explanation: This will add "painting" and "cooking" to the hobbies array field if they are not already present in the person object.

  2. $rename: Renames a field.

    const updateQuery = {
      $rename: {
        "person.name.last": "person.name.surname",
      },
    };

    Explanation: This will rename the last field to surname in the name object within the person object.

  3. $min: Updates the value of the field to the specified value if the specified value is less than the current value of the field.

    const updateQuery = {
      $min: {
        "person.age": 25,
      },
    };

    Explanation: This will update the age field to 25 if its current value is greater than 25 in the person object.

  4. $max: Updates the value of the field to the specified value if the specified value is greater than the current value of the field.

    const updateQuery = {
      $max: {
        "person.age": 40,
      },
    };

    Explanation: This will update the age field to 40 if its current value is less than 40 in the person object.

  5. $mul: Multiplies the value of the field by the specified value.

    const updateQuery = {
      $mul: {
        "person.age": 2,
      },
    };

    Explanation: This will multiply the age field by 2 in the person object.

  6. $inc: Increments the value of the field by the specified amount.

    const updateQuery = {
      $inc: {
        "person.age": 5,
      },
    };

    Explanation: This will increment the age field by 5 in the person object.

Apologies for the oversight. Let's cover the remaining operations:

  1. $bit: Performs bitwise AND, OR, and XOR updates on integral fields.

    const updateQuery = {
      $bit: {
        "person.age": { and: 5 }, // Performs a bitwise AND operation with 5
      },
    };

    Explanation: This will perform a bitwise AND operation with 5 on the age field in the person object.

  2. $currentDate: Sets the value of a field to the current date or time, either as a Date or a timestamp.

    const updateQuery = {
      $currentDate: {
        "person.lastModified": true, // Sets the field to the current date
      },
    };

    Explanation: This will set the lastModified field to the current date in the person object.

  3. $pop: Removes the first or last element of an array.

    const updateQuery = {
      $pop: {
        "person.hobbies": 1, // Removes the last element
      },
    };

    Explanation: This will remove the last element from the hobbies array field in the person object.

  4. $slice: Limits the number of elements in an array.

    const updateQuery = {
      $slice: {
        "person.hobbies": 2, // Limits the array to the first 2 elements
      },
    };

    Explanation: This will limit the hobbies array field to contain only the first 2 elements in the person object.

  5. $sort: Sorts the elements of an array.

    const updateQuery = {
      $sort: {
        "person.hobbies": 1, // Sorts the array in ascending order
      },
    };

Aggregate Operation Keys (AOPK)

Revised Example JSON Data:

[
  "data": [
    {
      "_id": 1,
      "name": "John",
      "age": 30,
      "skills": ["JavaScript", "Python"],
      "address": {
        "city": "New York",
        "country": "USA"
      }
    },
    {
      "_id": 2,
      "name": "Alice",
      "age": 25,
      "skills": ["Java", "C++"],
      "address": {
        "city": "London",
        "country": "UK"
      }
    }
  ]
]

Usage for Each Operation:

  1. $match:

    • This stage filters the documents to pass only the documents that match the specified condition(s).
    • It uses the matchesQuery method to check if a document matches the given query.
    • Example: { $match: { age: { $gt: 25 } } }
  2. $unwind:

    • This stage deconstructs an array field from the input documents to output a document for each element.
    • It helps in flattening nested arrays.
    • Example: { $unwind: "arrayField" }
  3. $group:

    • This stage groups documents by a specified identifier expression and applies the specified accumulator expressions.
    • It's typically used with aggregate functions like $sum, $avg, $min, $max, $first, $last, $addToSet, and $push.
    • Example: { $group: { _id: "field", total: { $sum: "value" } } }
  4. $addFields:

    • This stage adds new fields to documents.
    • It's useful for including calculated fields or modifying existing fields.
    • Example: { $addFields: { newField: expression } }
  5. $project:

    • This stage reshapes each document in the stream, including, excluding, or renaming fields.
    • It's akin to a SELECT statement in SQL.
    • Example: { $project: { field1: 1, field2: 1, _id: 0 } }
  6. $facet:

    • This stage allows for running multiple aggregation pipelines within a single stage on the same set of input documents.
    • It's useful for generating multiple sets of aggregation results.
    • Example: { $facet: { facet1: [pipeline1], facet2: [pipeline2] } }
  7. $redact:

    • This stage restricts the content for each document based on information stored in the document itself.
    • It's commonly used for implementing data access control.
    • Example: { $redact: expression }
  8. $bucket:

    • This stage categorizes incoming documents into groups, called buckets, based on a specified expression.
    • It's helpful for creating histograms or frequency distributions.
    • Example: { $bucket: { groupBy: "field", boundaries: [bound1, bound2], default: "other" } }
  9. $lookup:

    • This stage performs a left outer join to another collection in the same database to filter in documents from the "joined" collection for processing.
    • It's useful for resolving references across documents.
    • Example: { $lookup: { from: "collection", localField: "field", foreignField: "_id", as: "alias" } }
  10. $sample:

    • This stage randomly selects a specified number of documents from its input.
    • It's used for random sampling of data.
    • Example: { $sample: { size: 5 } }
  11. $sort:

    • This stage sorts the documents based on the specified fields.
    • It can be ascending (1) or descending (-1).
    • Example: { $sort: { field: 1 } }
  12. $limit:

    • This stage limits the number of documents passed to the next stage in the pipeline.
    • It's useful for pagination or restricting the result set size.
    • Example: { $limit: 10 }
  13. $skip:

    • This stage skips a specified number of documents.
    • It's useful for implementing pagination.
    • Example: { $skip: 10 }
  14. $out:

    • This stage writes the resulting documents of the aggregation pipeline to a specified collection.
    • It's used for storing the aggregated data in a separate collection.
    • Example: { $out: "outputCollection" }

VERSE.DB. Powered by JEDI Studio.