Preface
Fuzzy query is one of the basic operations of database, which can realize whether the given string matches the specified pattern. If the characters match exactly, it can be represented by = equal sign. If the characters match partially, it can be regarded as a fuzzy query. In relational data, use the syntax of like ‘% fens%’ through SQL. So how to achieve the effect of fuzzy query in mongodb.
query criteria
Keyword | Explain |
---|---|
$or | Or relationship |
$nor | Or relationship reversal |
$gt | greater than |
$gte | Greater than or equal to |
$lt | less than |
$lte | Less than or equal to |
$ne | Not equal to |
$in | In multiple value ranges |
$nin | Not in range of values |
$all | Match multiple values in an array |
$regex | Regular for fuzzy query |
$size | Match array size |
$maxDistance | Range query, distance (LBS based) |
$mod | Modular operation |
$near | Neighborhood query, query nearby location (based on LBS) |
$exists | Whether the field exists |
$elemMatch | Match elements in inner array |
$within | Range query (LBS based) |
$box | Range query, rectangular range |
$center | Range query, circular range |
$centerSphere | Range query, spherical range |
$slice | Query the elements in the field collection (for example, the nth to the m elements after the first few) |
Fuzzy query
Precise query
//Mongodb database table
const systemUser = require('../../models/user');
systemUser.find({name:'xiaoming'}).exec(function(err,rs){}
Multi condition fuzzy query
//Mongodb database table
const systemUser = require('../../models/user');
//Keywords to query passed in from the front end
var name = req.query.name;
Var page = req.query.page 𞓜 1; // current number of pages
Var limitnums = 10; // specifies the number of queries per page
page = parseInt(page);
Var skipnums = (page - 1) * limitnums; // skip the specified number
//Regular match I ignore case
var reg = new RegExp(name, "i");
var _filter = {
//Multi field matching
$or: [
{name: {$regex: reg}},
{description: {$regex: reg}},
{owner: {$regex: reg}},
]
}
systemUser.find(_filter).
//Skip the specified amount of data
skip(skipNums).
//Specifies the number of records read from mongodb.
limit(limitNums).
sort({createTime:-1}).
exec(function(err,rs){}
summary
The above is the whole content of this article. I hope that the content of this article has some reference learning value for your study or work. Thank you for your support for developepaer.