1.3.4 • Published 7 years ago

mongoose-extended-filter v1.3.4

Weekly downloads
2
License
ISC
Repository
github
Last release
7 years ago

mongoose-extended-filter Build Status

Extended filter for Mongoose

Use

const mongoose = require('mongoose');
const mongooseExtendedFilter = require('mongoose-extended-filter');

const treeSchema = new mongoose.Schema({
  name: String
});

const branchSchema = new mongoose.Schema({
  name: String,
  tree: {
    type: ObjectId,
    ref: 'Tree'
  }
});

treeSchema.plugin(mongooseExtendedFilter);
branchSchema.plugin(mongooseExtendedFilter);

const Tree = mongoose.model('Tree', treeSchema);
const Branch = mongoose.model('Branch', branchSchema);

Branch.prepareConditions({tree: {name: 'Billy'}})
  .then(conditions => {
    // conditions = {tree: {$in: [...]}
    return Branch.find(conditions).exec();
  })
  .then(branches => {
    // some code
  });

// or with dot-notation
Branch.prepareConditions({'tree.name': 'Billy'})
  .then(conditions => {
    // conditions = {tree: {$in: [...]}
    return Branch.find(conditions).exec()
  })
  .then(branches => {
    // some code
  });