Closed albertyou2 closed 2 years ago
For me, I had to use the model to create an aggregate first, then pass that to aggregatePaginate
, like this...
// ...
let actsRes = await app.model.StuActivate.aggregatePaginate(app.model.StuActivate.aggregate([
// ...
The examples in the readme show this as well, but at first I didn't look at it carefully enough, and missed the extra call to myModel.aggregate();
.
var myModel = require('/models/samplemodel');
const options = {
page: 1,
limit: 10
};
var myAggregate = myModel.aggregate();
myModel.aggregatePaginate(myAggregate, options).then(function(results){
console.log(results);
}).catch(function(err){
console.log(err);
})
I am actually having this issue. I followed the documentation. Here's my piece of code:
const postData =
await Posts.aggregate([
{ $match: { postingType: "posted" } },
{
$lookup: {
from: 'users',
localField: 'authorID',
foreignField: '_id',
as: 'user',
},
},
{ $unwind: { path: '$user', preserveNullAndEmptyArrays: true } },
{
$addFields:{ "user": "$user" }
}
])
const options = {
page: page,
limit: 10
};
Posts
.aggregatePaginate(postData, options)
.then(function (results) {
console.log(results);
res.status(200).json(results);
})
.catch(function (err) {
console.log(err);
});
My result is without the "user" object. Here is an object of the whole array of objects:
{
_id: 609589cec6ee911020e1c24e,
tags: [Array],
status: 'active',
authorID: 6079a8cdfc75353a23688cd7,
...
}
But when I console postData which I feed into aggregatePaginate()
, I get the user object:
{
...
user: {
_id: 6079a8cdfc75353a23688cd7,
type: 'user',
...
},
I used the $addFields stage to see if it would preserve the "user" object, but NO
I am actually having this issue. I followed the documentation. Here's my piece of code:
const postData = await Posts.aggregate([ { $match: { postingType: "posted" } }, { $lookup: { from: 'users', localField: 'authorID', foreignField: '_id', as: 'user', }, }, { $unwind: { path: '$user', preserveNullAndEmptyArrays: true } }, { $addFields:{ "user": "$user" } } ]) const options = { page: page, limit: 10 }; Posts .aggregatePaginate(postData, options) .then(function (results) { console.log(results); res.status(200).json(results); }) .catch(function (err) { console.log(err); });
My result is without the "user" object. Here is an object of the whole array of objects:
{ _id: 609589cec6ee911020e1c24e, tags: [Array], status: 'active', authorID: 6079a8cdfc75353a23688cd7, ... }
But when I console postData which I feed into
aggregatePaginate()
, I get the user object:{ ... user: { _id: 6079a8cdfc75353a23688cd7, type: 'user', ... },
I used the $addFields stage to see if it would preserve the "user" object, but NO
I just discovered that the problem was the await placed right before the aggregate():
**await** Posts.aggregate([])
After I removed the await, I got the required response (with the "user" object)
@albertyou2, @aravindnc Can you close this issue as the bug is resolved.
Thanks.
I am actually having this issue. I followed the documentation. Here's my piece of code:
const postData = await Posts.aggregate([ { $match: { postingType: "posted" } }, { $lookup: { from: 'users', localField: 'authorID', foreignField: '_id', as: 'user', }, }, { $unwind: { path: '$user', preserveNullAndEmptyArrays: true } }, { $addFields:{ "user": "$user" } } ]) const options = { page: page, limit: 10 }; Posts .aggregatePaginate(postData, options) .then(function (results) { console.log(results); res.status(200).json(results); }) .catch(function (err) { console.log(err); });
My result is without the "user" object. Here is an object of the whole array of objects:
{ _id: 609589cec6ee911020e1c24e, tags: [Array], status: 'active', authorID: 6079a8cdfc75353a23688cd7, ... }
But when I console postData which I feed into
aggregatePaginate()
, I get the user object:{ ... user: { _id: 6079a8cdfc75353a23688cd7, type: 'user', ... },
I used the $addFields stage to see if it would preserve the "user" object, but NO
I just discovered that the problem was the await placed right before the aggregate():
**await** Posts.aggregate([])
After I removed the await, I got the required response (with the "user" object)
It really works thanks
nodejs : v11.15.0 mongoose : v5.5.2 mongodb server : 4.0.3
hi @aravindnc : I was try to use $lookup and $project with mongoose-aggregate-paginate-v2. This is my query code :