node.js基于mongodb的搜索分页示例
MongoDB模糊查询并分页

创新互联主要从事网页设计、PC网站建设(电脑版网站建设)、wap网站建设(手机版网站建设)、成都响应式网站建设公司、程序开发、网站优化、微网站、微信小程序定制开发等,凭借多年来在互联网的打拼,我们在互联网网站建设行业积累了丰富的网站建设、成都网站设计、网站设计、网络营销经验,集策划、开发、设计、营销、管理等多方位专业化运作于一体。
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);
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。
分享文章:node.js基于mongodb的搜索分页示例
分享路径:http://www.jxjierui.cn/article/pgsooh.html


咨询
建站咨询
