当前位置:网站首页>Nodejs: mongodb simple fuzzy + paging query instance
Nodejs: mongodb simple fuzzy + paging query instance
2022-07-28 10:55:00 【shunzi2016】
function search(where, page_obj, next) {
var sort = { 'timestamp.create': 1 };
if (!where) {
where = {};
}
// var str=".*"+searchValue+".*$"
// var reg = new RegExp(str)
var reg_address = new RegExp('');
var reg_vm_name = new RegExp('');
var reg_asset = new RegExp('');
// Multi field matching
var _filter = { $or: [] };
if (where.address) {
reg_address = new RegExp(where.address);
_filter['$or'].push({
'address': { $regex: reg_address }
});
};
if (where.vm_name) {
reg_name = new RegExp(where.name);
_filter['$or'].push({
'name': { $regex: reg_name }
});
};
if (where.asset) {
reg_asset = new RegExp(where.asset);
_filter['$or'].push({
'asset': { $regex: reg_asset }
});
};
if (_filter['$or'].length == 0) {
_filter = {};
}
mongo.selectWhereGetCount(db_blist, _filter, (err, count) => {
if (err) throw err;
mongo.selectWhereOrderSkipLimit(db_studentlist,
_filter, {}, sort, page_obj.size, (page_obj.page - 1) * page_obj.size,
(err, result) => {
ret = {
'page': page_obj,
'total': count,
'rows': result
}
next(err, ret);
});
});
}
function selectWhereGetCount(collection, where, next) {
var dbo = db.db(db_name);
dbo.collection(collection).find(where).count(function(err, count) {
next(err, count);
});
}
function selectWhereOrderLimit(collection, where, fields, sort, limit, next) {
var dbo = db.db(db_name);
// sort = {'account_order':1};
// limit = 10; Pagination
console.log(sort, limit);
dbo.collection(collection).find(where, fields).sort(sort).limit(limit).toArray((err, result) => { // Returns all data in the collection
if (err) throw err;
next(err, result);
});
}边栏推荐
- GKRandom
- GKNoiseMap
- nodejs:检测并安装npm模块,如果已安装则跳过
- If you don't climb mountains, you don't know the height of the sky; If you don't face deep streams, you don't know the thickness of the earth
- An example of SQL trace in MySQL
- nodemcu之开发环境配置
- OCR 知识 概括
- MySQL Architecture Principle
- GKCoherentNoiseSource
- Go memory model (version on May 31st, 2014)
猜你喜欢

盘点:144个免费学习网站,全网最全资源合集

GKBillowNoiseSource

GKNoiseMap

Blue Bridge Cup embedded Hal library USART_ TX

Inventory: exciting data visualization chart

Redis-day01-常识补充及redis介绍

ctf技能树----文件上传

Blue Bridge Cup embedded Hal library USART_ RX

适合中小企业的进销存软件,搞定5大难题

5. Implement MapReduce program on window side to complete wordcount function
随机推荐
The future of generating confrontation networks in deepfake
非关系型数据库MongoDB的特点及安装
GKObstacle
JDBC各个类的解释
OCR knowledge summary
GKNoiseSource
这里有一份超实用Excel快捷键合集(常用+八大类汇总)
盘点:令人心动的数据可视化图表
Tree Shaking和DCE
Status Notice ¶
ctf技能树----文件上传
JSON preliminary understanding
float浮动初步理解
_ HUGE and __ IMP__ HUGE in “math.h“
蓝桥杯电子类嵌入式第十届省赛
国内外优秀程序员的博客全在这了,请查收
Andorid development
Yan reported an error: could not find any valid local directory for nmprivate/
5. Implement MapReduce program on window side to complete wordcount function
Install MySQL based on docker