I am trying to show the aggregated result of a mongodb embedded document. I want to query the embedded document on certain query parameters and show the paginated result of only subdocuments. But i am having some issue showing the data. The current format of the collection is as follows;
{
"_id" : ObjectId("5944b9d81b7bfc4d925d9e53"),
"deleted" : false,
"album_name" : "pokhara trip",
"album_description" : "album description of my-album",
"active" : "true",
"url_slog" : "pokhara-trip",
"images" : [
{
"_id" : ObjectId("5944bd111b7bfc4d925d9e55"),
"deleted" : true,
"image_title" : "test image 1",
"image_alt_text" : "test image alt text",
"image_description" : "test image detail",
"active" : "true"
},
{
"_id" : ObjectId("5944bd591b7bfc4d925d9e56"),
"deleted" : true,
"image_title" : "test image 1",
"image_alt_text" : "test image alt text",
"image_description" : "test image detail",
"active" : "true"
}
]
}
I am using aggregation framework to query the above collection and show only the images subdocument. and i want to show only those documents that are not marked as deleted.
I have done following to query and show the results. but i am having issues with the pagination and querying the subdocument.
below is my code;
const queryOpts = {};
const queryOptsImage = {};
queryOpts._id = ObjectId(req.params.albumId);
queryOpts.deleted = false;
queryOptsImage["images"] = {
'deleted': false
};
console.log(queryOpts);
console.log('--------------------------------------');
const galleryImages = await req.db.collection('ImageGalleryAlbum').aggregate(
{
$unwind:"$images"
},
{
$match: queryOpts
},
{
$project: projectionImageFields
},
{
$group: {
_id: null,
images: { $push: "$images" }
}
},
{
$unwind:"$images.images"
},
// {
// $limit:pagerOpts.perPage
// },
// {
// $skip:pagerOpts.perPage * (pagerOpts.page-1)
// },
).toArray();
// const imagesArr =
console.log(galleryImages);
And i am getting below result:
[
{
"images": [
{
"_id": "5944bd111b7bfc4d925d9e55",
"image_title": "test image 1",
"image_alt_text": "test image alt text",
"image_description": "test image detail",
"active": "true"
},
{
"_id": "5944bd591b7bfc4d925d9e56",
"image_title": "test image 1",
"image_alt_text": "test image alt text",
"image_description": "test image detail",
"active": "true"
}
]
}
]
What i want is that i want those images documents at the top level array. without { and images array.