node.js基于mongodb的搜索分頁示例
更新時(shí)間:2017年01月22日 15:02:30 作者:luckyGDD
本篇文章主要介紹了node.js基于mongodb的搜索分頁示例,mongodb分頁很簡(jiǎn)單,這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下。
mongodb模糊查詢并分頁
1.建立數(shù)據(jù)庫(kù)
代碼如下:
var mongoose = require('mongoose');
var shortid = require('shortid');
var Schema = mongoose.Schema;
var IndexDataSchema = new Schema({
_id: {
type: String,
unique: true,
'default': shortid.generate
},
type: String,
city: String,
name:string,
value: [{name: String, value: String}],
create: {type: Date, default: Date.now},
expand: String
});
IndexDataSchema.statics = {
defaultSort: {'create': 1},
defaultOptions: {'pageSize': 0}
};
var IndexData = mongoose.model('IndexData', IndexDataSchema);
module.exports = IndexData;
頁面布局 這里只需要搜索框和搜索按鈕,再點(diǎn)擊按鈕時(shí),執(zhí)行search()方法并發(fā)送請(qǐng)求
代碼如下:
<div class="searchPart">
<input type="text" class="form-control" id="txtSearch" placeholder="請(qǐng)輸入項(xiàng)目名稱">
<button class="btn btn-success search_btn" onclick="search()">搜索</button>
</div>
<script>
var paginObj;
//設(shè)置每頁顯示頁碼
var pageSize = 20;
//設(shè)置當(dāng)前頁碼為1
var currentPage = 1;
var condition = {'city': currentCityId, 'name': ''}
jQuery(document).ready(function () {
refresh();
});
//獲取查找條件
function getCondition() {
var name = $('#txtSearch').val();
if (name && name.trim()) {
// {'$regex': name, '$options': 'i'}}為模糊查詢固定語法,name為參數(shù)
condition = {'city': currentCityId, 'name': {'$regex': name, '$options': 'i'}};
}
else {
condition = {'city': currentCityId}
}
return condition;
}
//刷新頁面
function refresh() {
//查找內(nèi)容
$.get('/Manage/list/projects', {
'pageSize': pageSize,
'currentPage': currentPage,
'condition': getCondition()
}, function (result) {
appendData(result.data);
})
//查找個(gè)數(shù)
$.get('/Manage/listCount/projects', {'condition': condition}, function (result) {
paginObj = new DataPagin(document.querySelector('.projects-list'), result.count, {
'pageSize': pageSize,
'changePageFun': rquestPageData
});
})
}
//重新分頁
function rquestPageData(currentPage, callback) {
$.get('/Manage/list/projects', {
'pageSize': pageSize,
'currentPage': currentPage,
'condition': getCondition()
}, function (result) {
appendData(result.data);
})
if (callback) {
callback();
}
}
//改變頁碼,顯示相應(yīng)的內(nèi)容
function changePage(paginObj, index) {
paginObj.setPageNumber(index);
}
function appendData(data) {
//debugger;
var list = $('.projects-list').children('tbody');
list.html('');
// 頁面顯示模板
for (var i = 0; i < data.length; i++) {
.........
//此部分自己定義
})
}
//點(diǎn)擊搜索按鈕執(zhí)行該方法
function search() {
currentPage = 1;
refresh();
}
</script>
到數(shù)據(jù)庫(kù)查找并返回相應(yīng)內(nèi)容
var formidable = require("formidable");
var common = require('./common');
var path = require("path");
var fs = require('fs');
var path = require('path');
var guid = require('guid');
var shortid = require('shortid');
var AuctionHouse = require('./db/IndexData');
var funs = {
getList: function (collectionName, req, res, next) {
var mainObj = transformCollctionName(collectionName);
if (!mainObj) {
next();
}
var options = req.query;
var sort = options.sort || mainObj.defaultSort;
var pageSize = options.pageSize || mainObj.defaultOptions.pageSize;
var currentPage = options.currentPage || 1;
var condition = options.condition || {}
//此部分為查找條件
mainObj.find(condition).sort(sort).skip((currentPage - 1) * pageSize).limit(pageSize).exec(function (err, docs) {
if (err) {
next(err);
}
return res.json(common.returnData(true, docs));
})
},
countList: function (collectionName, req, res, next) {
var mainObj = transformCollctionName(collectionName);
if (!mainObj) {
next();
}
var condition = req.query.condition || {}
mainObj.find(condition).count().exec(function (err, docs) {
if (err) {
next(err);
}
return res.json({'count': docs});
})
},
HandleEvent: function (collectionName, actionsName, req, res, next) {
var mainFuns = getCollctionFuns(collectionName)
if (!mainFuns) {
next();
}
var fun = mainFuns[actionsName];
if (!fun) {
next();
}
fun(req, res, next);
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- nodeJS與MySQL實(shí)現(xiàn)分頁數(shù)據(jù)以及倒序數(shù)據(jù)
- Vue+Node實(shí)現(xiàn)商品列表的分頁、排序、篩選,添加購(gòu)物車功能詳解
- NodeJs操作MongoDB教程之分頁功能以及常見問題
- Node.js中Bootstrap-table的兩種分頁的實(shí)現(xiàn)方法
- nodejs mysql 實(shí)現(xiàn)分頁的方法
- nodejs個(gè)人博客開發(fā)第六步 數(shù)據(jù)分頁
- NodeJS和BootStrap分頁效果的實(shí)現(xiàn)代碼
- nodejs分頁類代碼分享
- node+express實(shí)現(xiàn)分頁效果
相關(guān)文章
node 安裝 windows-build-tools全過程
這篇文章主要介紹了node 安裝 windows-build-tools全過程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
nodejs構(gòu)建本地web測(cè)試服務(wù)器 如何解決訪問靜態(tài)資源問題
這篇文章主要為大家詳細(xì)介紹了nodejs構(gòu)建本地web測(cè)試服務(wù)器,教大家如何解決訪問靜態(tài)資源問題,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
Grunt針對(duì)靜態(tài)文件的壓縮,版本控制打包的實(shí)例講解
下面小編就為大家?guī)硪黄狦runt針對(duì)靜態(tài)文件的壓縮,版本控制打包的實(shí)例講解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09

