Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Multi-conditional Fuzzy query in MongoDB

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

How to implement multi-conditional fuzzy query in MongoDB? I believe that many inexperienced people are at a loss about this, so this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Fuzzy query is one of the basic operations of the database, which realizes whether a given string matches the specified pattern. If the characters match exactly, it can be indicated by the = equal sign, and if the partial match is considered to be a fuzzy query. In relational data, the syntax of like'% fens%' is used through SQL. So how should we achieve the effect of fuzzy query in mongodb?

Query condition

Keyword description $or or relational $nor or relational inverse $gt greater than $gte greater than $lt less than $lte less than $ne not equal to $in in multiple value ranges $nin is not within multiple value ranges $all matching array multiple values $regex regular, used for fuzzy query $size matching array size $maxDistance range query, distance (based on LBS) $mod modular operation $near neighborhood query Query nearby location (based on LBS) whether the $exists field exists $elemMatch matches the elements in the array $within range query (based on LBS) $box range query, rectangular range $center range query, circular range $centerSphere range query, spherical range $slice query elements in the field collection (for example, elements N to M after the number)

Fuzzy query

Accurate query

/ / Mongodb database table const systemUser = require ('.. /.. / models/user'); systemUser.find ({name:'xiaoming'}) .exec (function (err,rs) {})

Multi-conditional fuzzy query

/ / Mongodb database table const systemUser = require ('.. /.. / models/user'); / / keyword var name = req.query.name;var page = req.query.page passed in the front end | | 1; / / current number of pages var limitNums = 10; / / specify the number of queries per page page = parseInt (page); var skipNums = (page-1) * limitNums / / Skip 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 number of data skip (skipNums). / / specify the number of records to be read from MongoDB. Limit (limitNums). Sort ({createTime:-1}) .exec (function (err,rs) {} after reading the above, do you know how to implement multi-conditional fuzzy query in MongoDB? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 212

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report