Quantcast
Channel: CNode:Node.js专业中文社区
Viewing all articles
Browse latest Browse all 14821

学着写了个mongoose分页插件

$
0
0

https://github.com/sanrudongfeng/mongoose-page-query mongo数据库user集合,user对应的model为User(mongoose有schema,model,entity),假定有五条数据: [ {name: ‘a1’ , age: 3} , {name: ‘a2’ , age: 5} , {name: ‘a3’ , age: 1} , {name: ‘a4’ , age: 2} , {name: ‘a5’ , age: 4} ] 使用方式:

  1. npm install mongoose-page-query;
  2. mongoose.plugin(require(’ mongoose-page-query’));
  3. User.pageQuery({page:1,row:3},{},{},{sort:-1});//后三个参数对应mongodb中查询的query,projection,option
  4. user.pageQuery为async函数:
       async function test(){
  	     let userList =await User.pageQuery({page:1,row:3},{},{},{sort:{age:-1});
  	}

userList的返回结果为: {page,row,rows:[],total}四个参数,四个参数的意思都很明显,返回结果为: { total:5, page:1 , row:3, rows:[ {name: ‘a2’ , age: 5}, {name: ‘a5’ , age: 4}, {name: ‘a1’ , age: 3} ]} 第一次写,,,希望大家多多批评指正,有很多不足的地方,原来写个简单的东西都不容易。。。


Viewing all articles
Browse latest Browse all 14821

Trending Articles