
Research
Two Malicious Rust Crates Impersonate Popular Logger to Steal Wallet Keys
Socket uncovers malicious Rust crates impersonating fast_log to steal Solana and Ethereum wallet keys from source code.
@selieshjks/aggregate-query-bulder
Advanced tools
A MongoDB aggregation query builder for Mongoose models
Aggregate Query Builder is a powerful and flexible library that simplifies the creation of MongoDB aggregation pipelines. With built-in support for sub-pipelines, conditional logic, and automatic handling of Mongoose schema references, it helps developers streamline complex aggregation operations.
Whether you’re working with simple filters or complex queries with multiple conditions, Aggregate Query Builder allows you to build, organize, and reuse MongoDB aggregation stages in a clean and readable way.
matchIf
with sub-pipelines for flexible condition-based queries.$lookup
handling: Automatically fetch referenced collections based on Mongoose model schema.$match
, $group
, $project
, $sort
, $facet
, and more.To get started, simply install the package from npm:
npm install @selieshjks/aggregate-query-builder
const mongoose = require("mongoose");
const AggregateQueryBuilder = require("@selieshjks/aggregate-query-builder");
const model = mongoose.model("User", new mongoose.Schema({
name: String,
age: Number,
city: String,
friendId: { type: mongoose.Schema.Types.ObjectId, ref: "Friend" }
}));
const builder = new AggregateQueryBuilder(model);
// Create sub-pipelines
builder.matchEqual("age", { $gte: 30 }, "ageFilter");
builder.matchEqual("city", "New York", "cityFilter");
builder.matchEqual("height",{$lte: 160}, "heightFilter");
// Use sub-pipelines in matchIf
builder.matchIf("ageFilter", "cityFilter", "heightFilter");
// Build and output the final pipeline
const pipeline = builder.build();
console.log(JSON.stringify(pipeline, null, 2));
import mongoose, { Model } from "mongoose";
import AggregateQueryBuilder from "@selieshjks/aggregate-query-builder";
const model: Model<any> = mongoose.model(
"User",
new mongoose.Schema({
name: String,
age: Number,
city: String,
friendId: { type: mongoose.Schema.Types.ObjectId, ref: "Friend" },
})
);
const builder = new AggregateQueryBuilder(model);
// Define sub-pipelines
builder.matchEqual("age", { $gte: 30 }, "ageFilter");
builder.matchEqual("city", "New York", "cityFilter");
builder.matchEqual("height",{$lte: 160}, "heightFilter");
// Use sub-pipelines in conditional match
builder.matchIf("ageFilter", "cityFilter", "heightFilter");
// Build the pipeline and log it
const pipeline = builder.build();
console.log(JSON.stringify(pipeline, null, 2));
matchEqual(field, value, subPipelineName)
$match
stage for equality condition.field
: The field to match.value
: The value to match against.subPipelineName
: The name of the sub-pipeline. This is a required parameter.matchRange(field, operator, value, subPipelineName)
$match
stage for range conditions (e.g., $gt
, $lt
).field
: The field to match.operator
: The operator (e.g., $gt
, $lt
).value
: The value to compare.subPipelineName
: The name of the sub-pipeline. This is a required parameter.matchIf(subPipelineName, conditionKey, trueMatch, falseMatch)
$match
stage with conditional logic using sub-pipelines.subPipelineName
: The name of the sub-pipeline containing the condition (trueMatch
and falseMatch
).conditionKey
: The key used to retrieve the condition from the sub-pipeline.trueMatch
: The match condition to apply if the condition is true
.falseMatch
: The match condition to apply if the condition is false
.matchSwitch(subPipelineName, switchField, cases)
$match
stage with a switch
condition, based on a field and set of cases.subPipelineName
: The name of the sub-pipeline containing the cases.switchField
: The field to evaluate.cases
: An array of case objects where each object contains caseCondition
and then
condition.lookup(fields, subPipelineName)
$lookup
stages based on Mongoose schema references.fields
: The fields to lookup.subPipelineName
: The name of the sub-pipeline. This is a required parameter.unwind(fields, subPipelineName)
$unwind
stages to deconstruct arrays.fields
: The fields to unwind.subPipelineName
: The name of the sub-pipeline. This is a required parameter.group(groupConditions, subPipelineName)
$group
stage to group documents based on conditions.groupConditions
: The conditions for grouping.subPipelineName
: The name of the sub-pipeline. This is a required parameter.project(fields, subPipelineName)
$project
stage to specify fields to include or exclude.fields
: The fields to include or exclude.subPipelineName
: The name of the sub-pipeline. This is a required parameter.sort(sortConditions, subPipelineName)
$sort
stage to sort documents based on specified conditions.sortConditions
: The sort conditions (e.g., { age: 1 }
for ascending).subPipelineName
: The name of the sub-pipeline. This is a required parameter.appendSubPipeline(subPipelineName)
subPipelineName
: The name of the sub-pipeline to append.build()
Here’s a more complex example showing how to use sub-pipelines effectively:
const builder = new AggregateQueryBuilder(model);
// Create sub-pipelines
builder.matchEqual("age", { $gte: 30 }, "ageFilter");
builder.matchEqual("city", "New York", "cityFilter");
builder.matchEqual("height",{$lte: 160}, "heightFilter");
// Use sub-pipelines in a matchIf stage
builder.matchIf("ageFilter", "cityFilter", "heightFilter");
// Append the sub-pipeline and build the final pipeline
builder.appendSubPipeline();
const pipeline = builder.build();
console.log(JSON.stringify(pipeline, null, 2));
If you’d like to contribute, feel free to fork the repo, open an issue, or submit a pull request. Contributions are always welcome!
This project is licensed under the MIT License - see the LICENSE file for details.
Check out the project on GitHub: Aggregate Query Builder GitHub
Install from npm: @selieshjks/aggregate-query-builder
FAQs
A MongoDB aggregation query builder for Mongoose models
The npm package @selieshjks/aggregate-query-bulder receives a total of 5 weekly downloads. As such, @selieshjks/aggregate-query-bulder popularity was classified as not popular.
We found that @selieshjks/aggregate-query-bulder demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Socket uncovers malicious Rust crates impersonating fast_log to steal Solana and Ethereum wallet keys from source code.
Research
A malicious package uses a QR code as steganography in an innovative technique.
Research
/Security News
Socket identified 80 fake candidates targeting engineering roles, including suspected North Korean operators, exposing the new reality of hiring as a security function.