aravindnc / mongoose-aggregate-paginate-v2

A cursor based custom aggregate pagination library for Mongoose with customizable labels.
MIT License
131 stars 23 forks source link

mongoose-autopopulate not populating this but mongoose-paginate-v2 #16

Closed cybercoder closed 2 months ago

cybercoder commented 4 years ago

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.

solancer commented 4 years ago

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 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.

solancer commented 4 years ago

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)
aravindnc commented 4 years ago

@solancer I guess it can be due to your mongoose-autopopulate plugin usage. I'm not sure though.