Fi Lter
MongoDB and Mongoose aggregation filter and search helper.
This module is intended as a simple abstraction layer to perform basic but powerful matching with a concatenation of one or multiple model's fields.
Diacritic Matching
The diacritic insensitive functionality is handled by Fi Di Regex. If you find an issue with this functionality, please open an issue in the Fi Di Regex Issues page.
Important
Although this module's functionality is nearly complete the documentation is still in progress. In the meantime, the docs may help you a bit.
Installation
npm i fi-lter
Remember to use --save
if you're using a NPM version less than 5.x.x.
Usage
const mongoose = require('mongoose');
const filter = require('fi-lter');
const queryText = 'google';
const KEYWORDS_SLUG = filter.keywordsSlug([
'$brand', '$model', '$color', '$serial', {
$substrBytes: ['$year', 0, -1]
}
]);
const KEYWORDS_GROUP = filter.keywordsGroup([
'brand', 'model', 'year', 'serial', 'price', 'createdAt', 'updatedAt'
]);
Device = mongoose.model('device', new mongoose.Schema({
brand: String,
model: String,
color: String,
serial: String,
year: Number,
price: Number
}, {
timestamps: true
}));
const query = Device.aggregate();
query.append(filter.byKeywords(queryText, KEYWORDS_SLUG, KEYWORDS_GROUP));
query.then(results => {
console.log(results);
}).catch((err) => {
console.error(err);
});