当前位置:网站首页>Nodejs:mongodb 简单模糊+分页查询实例
Nodejs:mongodb 简单模糊+分页查询实例
2022-07-28 10:28: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('');
//多字段匹配
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;分页用
console.log(sort, limit);
dbo.collection(collection).find(where, fields).sort(sort).limit(limit).toArray((err, result) => { // 返回集合中所有数据
if (err) throw err;
next(err, result);
});
}边栏推荐
- 第11届蓝桥杯本科组校赛(20200321)
- [application of stack] - infix expression to suffix expression
- GKObstacle
- 两年CRUD,二本毕业,备战两个月面试阿里,侥幸拿下offer定级P6
- PyQt5快速开发与实战 4.12 日历与时间
- Tensorflow knowledge points
- Semeval 2022 | introducing knowledge into ner system, aridamo academy won the best paper award
- 2019年9月PAT甲级题目
- markdown转成word或者pdf
- 6. MapReduce custom partition implementation
猜你喜欢

7. MapReduce custom sorting implementation
![Implement a queue with two stacks [C language]](/img/8a/679575bb0a562eff7e4317e64b4790.png)
Implement a queue with two stacks [C language]

Uni app project directory, file function introduction and development specification

3. MapReduce explanation and source code analysis

Characteristics and installation of non relational database mongodb

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

ICML 2022 | graph represents the structure aware transformer model of learning

11_ue4进阶_男性角色换成女性角色,并修改动画

SQL Server 2016 learning records - set query

Machine learning -- handwritten English alphabet 1 -- classification process
随机推荐
Attention attention mechanism flow chart
10_ UE4 advanced_ Add fall and cast actions
蓝桥杯嵌入式-HAL库-USART_RX
试题 历届试题 发现环
GKCoherentNoiseSource
GKSpheresNoiseSource
蓝桥杯嵌入式-HAL库-SYSTICK
JDBC各个类的解释
Start from scratch blazor server (2) -- consolidate databases
An example of SQL trace in MySQL
GKBillowNoiseSource
GKRidgedNoiseSource
Inverse element & combinatorial number & fast power
2020第二届传智杯初赛
GKCircleObstacle
GKObstacle
Pyqt5 rapid development and practice 4.11 drag and clipboard
AP AUTOSAR platform design 3 architecture
The future of generating confrontation networks in deepfake
GKSpheresNoiseSource