您好,登錄后才能下訂單哦!
mongodb模糊查詢并分頁
1.建立數據庫
代碼如下:
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;
頁面布局 這里只需要搜索框和搜索按鈕,再點擊按鈕時,執行search()方法并發送請求
代碼如下:
<div class="searchPart"> <input type="text" class="form-control" id="txtSearch" placeholder="請輸入項目名稱"> <button class="btn btn-success search_btn" onclick="search()">搜索</button> </div> <script> var paginObj; //設置每頁顯示頁碼 var pageSize = 20; //設置當前頁碼為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為參數 condition = {'city': currentCityId, 'name': {'$regex': name, '$options': 'i'}}; } else { condition = {'city': currentCityId} } return condition; } //刷新頁面 function refresh() { //查找內容 $.get('/Manage/list/projects', { 'pageSize': pageSize, 'currentPage': currentPage, 'condition': getCondition() }, function (result) { appendData(result.data); }) //查找個數 $.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(); } } //改變頁碼,顯示相應的內容 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++) { ......... //此部分自己定義 }) } //點擊搜索按鈕執行該方法 function search() { currentPage = 1; refresh(); } </script>
到數據庫查找并返回相應內容
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); }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。