[node.js能做什么]node.js基于mongodb的搜索分页示例

更新时间:2021-06-09    来源:MongoDB    手机版     字体:

【www.bbyears.com--MongoDB】

mongodb模糊查询并分页

1.建立数据库

代码如下:

 

 代码如下

varmongoose = require('mongoose');

varshortid = require('shortid');

varSchema = mongoose.Schema;

 

varIndexDataSchema =newSchema({

  _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}

 

};

 

 

varIndexData = mongoose.model('IndexData', IndexDataSchema);

 

module.exports = IndexData;

 

页面布局 这里只需要搜索框和搜索按钮,再点击按钮时,执行search()方法并发送请求

代码如下:

 

 代码如下

          

          

        

        <script>

  varpaginObj;

   

  //设置每页显示页码

  varpageSize = 20;

    

  //设置当前页码为1

  varcurrentPage = 1;

   

  varcondition = {'city': currentCityId,'name':''}

  jQuery(document).ready(function() {

    refresh();

  });

   

 //获取查找条件

  functiongetCondition() {

    varname = $('#txtSearch').val();

    if(name && name.trim()) {

    // {'$regex': name, '$options': 'i'}}为模糊查询固定语法,name为参数

      condition = {'city': currentCityId,'name': {'$regex': name,'$options':'i'}};

    }

    else{

      condition = {'city': currentCityId}

    }

    returncondition;

  }

   

   //刷新页面

  functionrefresh() {

   

  //查找内容

    $.get('/Manage/list/projects', {

      'pageSize': pageSize,

      'currentPage': currentPage,

      'condition': getCondition()

    },function(result) {

      appendData(result.data);

    })

 

//查找个数

    $.get('/Manage/listCount/projects', {'condition': condition},function(result) {

      paginObj =newDataPagin(document.querySelector('.projects-list'), result.count, {

        'pageSize': pageSize,

        'changePageFun': rquestPageData

      });

    })

  }

 

//重新分页

  functionrquestPageData(currentPage, callback) {

    $.get('/Manage/list/projects', {

      'pageSize': pageSize,

      'currentPage': currentPage,

      'condition': getCondition()

    },function(result) {

      appendData(result.data);

    })

    if(callback) {

      callback();

    }

  }

 

//改变页码,显示相应的内容

  functionchangePage(paginObj, index) {

    paginObj.setPageNumber(index);

  }

 

  functionappendData(data) {

    //debugger;

    varlist = $('.projects-list').children('tbody');

    list.html('');

    // 页面显示模板

    for(vari = 0; i < data.length; i++) {

     

      .........

      //此部分自己定义

       

    

    })

  }

 

  

   //点击搜索按钮执行该方法

  functionsearch() {

    currentPage = 1;

    refresh();

  }

 

 

</script>

 

2.到数据库查找并返回相应内容

 

 代码如下

varformidable = require("formidable");

 varcommon = require('./common');

 varpath = require("path");

 varfs = require('fs');

 varpath = require('path');

 varguid = require('guid');

 varshortid = require('shortid');

  

 varAuctionHouse = require('./db/IndexData');

 

 

varfuns = {

 getList:function(collectionName, req, res, next) {

   varmainObj = transformCollctionName(collectionName);

   if(!mainObj) {

     next();

   }

   varoptions = req.query;

   varsort = options.sort || mainObj.defaultSort;

   varpageSize = options.pageSize || mainObj.defaultOptions.pageSize;

   varcurrentPage = options.currentPage || 1;

   varcondition = options.condition || {}

    

   //此部分为查找条件

   mainObj.find(condition).sort(sort).skip((currentPage - 1) * pageSize).limit(pageSize).exec(function(err, docs) {

     if(err) {

       next(err);

     }

     returnres.json(common.returnData(true, docs));

   })

 },

 countList:function(collectionName, req, res, next) {

   varmainObj = transformCollctionName(collectionName);

   if(!mainObj) {

     next();

   }

   varcondition = req.query.condition || {}

   mainObj.find(condition).count().exec(function(err, docs) {

     if(err) {

       next(err);

     }

     returnres.json({'count': docs});

   })

 },

 HandleEvent:function(collectionName, actionsName, req, res, next) {

   varmainFuns = getCollctionFuns(collectionName)

   if(!mainFuns) {

     next();

   }

   varfun = mainFuns[actionsName];

   if(!fun) {

     next();

   }

   fun(req, res, next);

 }

 

本文来源:http://www.bbyears.com/shujuku/122625.html

热门标签

更多>>

本类排行