mongoose-aggregate-paginate-v2
mongoose-aggregate-paginate-v2 copied to clipboard
mongoose-autopopulate not populating this but mongoose-paginate-v2
This is My Post Schema:
import mongoose, { Schema } from 'mongoose';
import mongoosePaginate from 'mongoose-paginate-v2';
import mongooseAggregatePaginate from 'mongoose-aggregate-paginate-v2';
import mongooseAutoPopulate from 'mongoose-autopopulate';
const PostSchema = new Schema(
{
author: {
type: Schema.Types.ObjectId,
ref: 'User',
required: true,
autopopulate: {
select: '-password -refreshTokenCount',
},
sparse: true,
},
title: {
type: String,
required: true,
},
body: {
type: String,
},
attachments: [{ filename: String, encoding: String, mimeType: String }],
location: {
type: {
type: String,
enum: ['Point'],
required: true,
},
coordinates: {
type: [Number],
required: true,
},
},
favs: {
type: Number,
default: 0,
},
tags: [String],
},
{ timestamps: false }
);
PostSchema.plugin(mongooseAutoPopulate);
PostSchema.plugin(mongoosePaginate);
PostSchema.plugin(mongooseAggregatePaginate);
export default mongoose.model('Post', PostSchema);
I am trying to fetch and paginate posts and only 20 first characters of body
field as intro
like this:
async list({ page = null, offset = null, limit = DefaultRowsLimit, tags = null }) {
let options = { limit };
!!page ? (options.page = page) : offset ? (options.offset = offset) : (options.offset = 0);
let a = PostModel.aggregate([
{
$project: {
author: '$author',
title: '$title',
body: { $substr: ['$body', 0, 20] },
location: '$location',
tags: '$tags',
},
},
]);
return await PostModel.aggregatePaginate(a, options);
}
But author
field not populate automatically.
I'm facing same issue too
This is My Post Schema:
import mongoose, { Schema } from 'mongoose'; import mongoosePaginate from 'mongoose-paginate-v2'; import mongooseAggregatePaginate from 'mongoose-aggregate-paginate-v2'; import mongooseAutoPopulate from 'mongoose-autopopulate'; const PostSchema = new Schema( { author: { type: Schema.Types.ObjectId, ref: 'User', required: true, autopopulate: { select: '-password -refreshTokenCount', }, sparse: true, }, title: { type: String, required: true, }, body: { type: String, }, attachments: [{ filename: String, encoding: String, mimeType: String }], location: { type: { type: String, enum: ['Point'], required: true, }, coordinates: { type: [Number], required: true, }, }, favs: { type: Number, default: 0, }, tags: [String], }, { timestamps: false } ); PostSchema.plugin(mongooseAutoPopulate); PostSchema.plugin(mongoosePaginate); PostSchema.plugin(mongooseAggregatePaginate); export default mongoose.model('Post', PostSchema);
I am trying to fetch and paginate posts and only 20 first characters of
body
field asintro
like this:async list({ page = null, offset = null, limit = DefaultRowsLimit, tags = null }) { let options = { limit }; !!page ? (options.page = page) : offset ? (options.offset = offset) : (options.offset = 0); let a = PostModel.aggregate([ { $project: { author: '$author', title: '$title', body: { $substr: ['$body', 0, 20] }, location: '$location', tags: '$tags', }, }, ]); return await PostModel.aggregatePaginate(a, options); }
But
author
field not populate automatically.
Ended up using $lookup & $project to achieve population
model.aggregate([
{
$lookup: {
from: 'User',
localField: 'requester',
foreignField: '_id',
as: 'requester'
}
},
{
$project: {
'requester.facebookProvider': 0,
'requester.googleProvider': 0,
}
},
])
resp = await db.getItems(req, model, query)
@solancer I guess it can be due to your mongoose-autopopulate plugin usage. I'm not sure though.