充满了猫鼬的分页

时间:2018-01-05 12:57:24

标签: node.js mongodb mongoose mongoose-populate

我尝试使用import xlsxwriter from openpyxl import Workbook from openpyxl import load_workbook #modified example from xlsxwriter documentation at http://xlsxwriter.readthedocs.io/chart.html data = [ [1, 2, 3, 4, 5], [2, 4, 6, 8, 10], [3, 6, 9, 12, 15], ] workbook = xlsxwriter.Workbook('Test_template.xlsx') worksheet = workbook.add_worksheet() chart = workbook.add_chart({'type': 'column'}) worksheet.write_column('A1', data[0]) worksheet.write_column('B1', data[1]) worksheet.write_column('C1', data[2]) chart.add_series({'values': '=Sheet1!$A$1:$A$5'}) chart.add_series({'values': '=Sheet1!$B$1:$B$5'}) chart.add_series({'values': '=Sheet1!$C$1:$C$5'}) worksheet.insert_chart('A10', chart) workbook.close() #opening xlsxwriter created workbook with openpyxl and adding a sheet workbook = load_workbook('Test_template.xlsx') worksheet = workbook.create_sheet('new_sheet') worksheet["A1"] = "Putting some text in" workbook.save('Test_template.xlsx') npm获取数据,但填充不起作用

这是我的mongoose-paginate

UsersSchema.js

这里是帖子架构

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var usersSchema = new Schema({
    name : String,
    created_at : { type : Date, default : Date.now }
});

module.exports = mongoose.model('users',usersSchema);

这是我的查询

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var mongoosePaginate = require('mongoose-paginate');

var postsSchema = new Schema({
    user : { type: Schema.Types.ObjectId, ref: 'users' },
    post : String,
    created_at : { type : Date, default : Date.now }
});
postsSchema.plugin(mongoosePaginate);
module.exports = mongoose.model('posts',postsSchema);

用户提供objectID而不是用户数据。 即

var options = {
    sort:     { created_at: -1 },
    lean:     true,
    offset:   offset,
    populate : 'users',
    limit:    10
};
postsSchema.paginate({user:user},options,function(err,posts){
    if(err){
        console.log(err)
        return false;
    }
    console.log(posts)
});

2 个答案:

答案 0 :(得分:9)

填充物有以下内容

Post.find({})
.populate([
    // here array is for our memory. 
    // because may need to populate multiple things
    {
        path: 'user',
        select: 'name',
        model:'User',
        options: {
            sort:{ },
            skip: 5,
            limit : 10
        },
        match:{
            // filter result in case of multiple result in populate
            // may not useful in this case
        }
    }
])
.exec((err, results)=>{
   console.log(err, results)
})

答案 1 :(得分:4)

如果要使用猫鼬分页,可以执行以下操作

var query = {};
var options = {
  sort: { date: -1 },
  populate: 'users',
  lean: true,
  offset: offset, 
  limit: 10
};

Post.paginate({}, options, (err, result) => {
 //....
})