node. js Example of search paging based on mongodb

  • 2021-07-13 04:07:31
  • OfStack

mongodb Fuzzy Query and Paging

STEP 1 Establish a database

The code is as follows:


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;

The layout of the page requires only the search box and the search button, and when the button is clicked again, the search () method is executed and the request is sent

The code is as follows:


 <div class="searchPart">
          <input type="text" class="form-control" id="txtSearch" placeholder=" Please enter a project name ">
          <button class="btn btn-success search_btn" onclick="search()"> Search </button>
        </div>
        <script>
  var paginObj;
  
  // Set the page number displayed per page 
  var pageSize = 20; 
   
  // Set the current page number to 1
  var currentPage = 1;
  
  var condition = {'city': currentCityId, 'name': ''}
  jQuery(document).ready(function () {
    refresh();
  });
  
 // Get lookup criteria 
  function getCondition() {
    var name = $('#txtSearch').val();
    if (name && name.trim()) {
    // {'$regex': name, '$options': 'i'}} Fixed syntax for fuzzy queries, name Is a parameter 
      condition = {'city': currentCityId, 'name': {'$regex': name, '$options': 'i'}};
    }
    else {
      condition = {'city': currentCityId}
    }
    return condition;
  }
  
   // Refresh page 
  function refresh() {
  
  // Find content 
    $.get('/Manage/list/projects', {
      'pageSize': pageSize,
      'currentPage': currentPage,
      'condition': getCondition()
    }, function (result) {
      appendData(result.data);
    })

// Number of searches 
    $.get('/Manage/listCount/projects', {'condition': condition}, function (result) {
      paginObj = new DataPagin(document.querySelector('.projects-list'), result.count, {
        'pageSize': pageSize,
        'changePageFun': rquestPageData
      });
    })
  }

// Re-paging 
  function rquestPageData(currentPage, callback) {
    $.get('/Manage/list/projects', {
      'pageSize': pageSize,
      'currentPage': currentPage,
      'condition': getCondition()
    }, function (result) {
      appendData(result.data);
    })
    if (callback) {
      callback();
    }
  }

// Change the page number to display the corresponding content 
  function changePage(paginObj, index) {
    paginObj.setPageNumber(index);
  }

  function appendData(data) {
    //debugger;
    var list = $('.projects-list').children('tbody');
    list.html('');
    //  Page display template 
    for (var i = 0; i < data.length; i++) {
    
      .........
      // This section defines itself 
      
   
    })
  }

 
   // Click the search button to execute the method 
  function search() {
    currentPage = 1;
    refresh();
  }


</script>

Look up the database and return the corresponding contents


 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 || {}
    
    // This section is the lookup 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);
  }

Related articles: