Mongoose Fuzzy Searching
mongoose-fuzzy-searching-v2 is simple and lightweight plugin that enables fuzzy searching in documents in MongoDB. This code is based on this article.
Features
Installation
Install using npm
npm i mongoose-fuzzy-searching-v2
Usage
Simple usage
In the below example, we have a User
collection and we want to make fuzzy searching in firstName
and lastName
.
var mongoose_fuzzy_searching = ; var UserSchema = firstName: String lastName: String email: String age: Number; UserSchema; var User = mongoose; var user = firstName: "Joe" lastName: "Doe" email: "joe.doe@mail.com" age: 30; user;
The results are sorted by the confidenceScore
key. You can override this option.
User ;
Plugin Options
Options must have a fields
key, which is an Array of Strings
or an Array of Objects
.
var mongoose_fuzzy_searching = ; var UserSchema = firstName: String lastName: String email: String; UserSchema;
Object keys
The below table contains the expected keys for an object
Example:
var mongoose_fuzzy_searching = ; var UserSchema = firstName: String lastName: String email: String text: title: String description: String language: String ;
fuzzySearch parameters
fuzzySearch
method can accept up to three parameters. The first one is the query, which can either be either a String
or an Object
. This parameter is required.
The second parameter can either be eiter an Object
with other queries, for example age: { $gt: 18 }
, or a callback function.
If the second parameter is the options, then the third parameter is the callback function. If you don't set a callback function, the results will be returned inside a Promise.
Example:
/* Without options and callback */Model ;// orModel ;// with prefixOnly and minSizeModel ; /* With options and without callback */Model ; /* With callback */Model; /* With options and callback */Model;
Work with pre-existing data
The plugin creates indexes for the selected fields. In the below example the new indexes will be firstName_fuzzy
and lastName_fuzzy
. Also, each document will have the fields firstName_fuzzy
[String] and lastName_fuzzy
[String]. These arrays will contain the anagrams for the selected fields.
var mongoose_fuzzy_searching = ; var UserSchema = firstName: String lastName: String email: String age: Number; UserSchema;
License
MIT License
Copyright (c) 2019 Vassilis Pallas
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.