Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

populate with mongoose pagination

i tried to fetch data using npm mongoose-paginate but populate is not working

here is my 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);

here is post schema

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

here is my query

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

user provide objectID not a users data. i.e

[{
   user : objectID(987654ff11aa),
   post : 'post'
}]  
like image 343
Kaushik Makwana Avatar asked Jan 05 '18 12:01

Kaushik Makwana


People also ask

How do you use Mongoose Paginate?

Try using mongoose function for pagination. Limit is the number of records per page and number of the page. Show activity on this post. var paginate = 20; var page = pageNumber; MySchema.

What is Mongoose Paginate v2?

mongoose-paginate-v2 is a pagination library having a page wrapper. The main usage of the plugin is you can alter the return value keys directly in the query itself so that you don't need any extra code for transformation.

What does populate method do mongoose?

Mongoose Populate() Method. In MongoDB, Population is the process of replacing the specified path in the document of one collection with the actual document from the other collection.

What is populate in mongoose?

Mongoose has a more powerful alternative called populate() , which lets you reference documents in other collections. Population is the process of automatically replacing the specified paths in the document with document(s) from other collection(s).


2 Answers

A populate have following things

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)
})
like image 57
shivshankar Avatar answered Sep 19 '22 11:09

shivshankar


If you want to use mongoose-paginate, You can do the following

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

Post.paginate({}, options, (err, result) => {
 //....
})
like image 27
Olu Udeh Avatar answered Sep 19 '22 11:09

Olu Udeh