栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

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

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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()方法并发送请求

代码如下:

 
   
   
 
 

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

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

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/90588.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号