当前位置:网站首页>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);
});
}边栏推荐
猜你喜欢

GKCheckerboardNoiseSource

Preliminary understanding of float

这里有一份超实用Excel快捷键合集(常用+八大类汇总)

5. Implement MapReduce program on window side to complete wordcount function

Blue Bridge Cup embedded Hal library ADC

Pyqt5 rapid development and practice 4.13 menu bar, toolbar and status bar and 4.14 qprinter

粒子群实现最优解的求解

盘点:令人心动的数据可视化图表

Sword finger offer 35. replication of complex linked list

表格数据处理软件,除了Excel还有什么?
随机推荐
剑指 Offer 30. 包含min函数的栈
GKCoherentNoiseSource
Ten questions about low code: tell everything about low code!
nodemcu之开发环境配置
剑指 Offer 09. 用两个栈实现队列
Eslint, Eslint中文文档
c语言进阶篇:指针(一)
The future of generating confrontation networks in deepfake
Sword finger offer 35. replication of complex linked list
GKRidgedNoiseSource
Andorid 开发三 (Intent)
Arduino Basics
Pyqt5 rapid development and practice 4.12 calendar and time
Invalid ROM Table原因及解决办法
Yan reported an error: could not find any valid local directory for nmprivate/
GKBillowNoiseSource
MySQL Architecture Principle
6. MapReduce custom partition implementation
ctf技能树----文件上传
Tree Shaking和DCE