欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

node.js基于mongodb的搜索分页示例

程序员文章站 2024-02-07 19:32:58
mongodb模糊查询并分页 1.建立数据库 代码如下: var mongoose = require('mongoose'); var shortid =...

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);
  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。