Notorious Penguin Magicians
    Wondering what’s next for npm?Check out our public roadmap! »

    mongoose-cast-aggregation

    0.1.2 • Public • Published

    Mongoose Cast Aggregation

    A mongoose plugin that casts aggregation pipelines whenever possible.

    Build Status Coverage Status NPM version

    NPM

    Getting Started

    Run:

    npm install mongoose-cast-aggregation
    

    Inject the plugin into mongoose:

    const mongoose = require('mongoose');
    const castAggregation = require('mongoose-cast-aggregation');
    
    mongoose.plugin(castAggregation); 

    Now mongoose will cast the $match stage whenever possible. It casts the $match stage as long as no stage before it changed the resulting document shape from the original schema (e.g. $match, $limit, $sort, $skip, and $sample). It also casts query on $geoNear stages.

    // After injecting the plugin
    const discountSchema = new Schema({
      expiresAt: Date,
      amount: Number
    });
    
    const Discount = mongoose.model('Discount', discountSchema);
    
    const discounts = await Discount.aggregate([
      // Will cast the amount to a number, and the timestamp to a date object
      { $match: { expiresAt: { $lt: Date.now() }, amount: '20' } }
    ]);

    This works as well:

    const discounts = await Discount.aggregate([
      { $sort: { amount:-1 } },
      { $skip: 20 },
      // Will cast the stage below to a date object, because the document shape hasn't changed yet.
      { $match: { expiresAt: { $lt: Date.now() } } },
    
      // Will cast this one to numbers as well.
      { $match: { amount: { $gt: '80', $lt: '200' } } },
      { $project: { amountInUSD: '$amount' } },
    
      // Will ***NOT*** cast this one, because we used a stage that changed the shape of the document.
      // so using the string '100' here will not work, will have to use the correct type of number in order to get results.
      { $match: { amountInUSD: { $gt: 100 } } }
    ]);

    Install

    npm i mongoose-cast-aggregation

    DownloadsWeekly Downloads

    712

    Version

    0.1.2

    License

    ISC

    Unpacked Size

    4.89 kB

    Total Files

    7

    Last publish

    Collaborators

    • avatar