0

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.

Shrawan Lakhe
  • 31
  • 1
  • 4
  • `queryOpts = { "_id": ObjectId(req.params.albumId), "images.deleted": false }`. See [Dot notation](https://docs.mongodb.com/manual/core/document/#arrays) in the core documentation. – Neil Lunn Jun 18 '17 at 05:46
  • thank you @Neil queryOpts as suggested by you works but i am getting the result in above mentioned format. how can i change it so that i get all the images as array of objects in single level – Shrawan Lakhe Jun 18 '17 at 05:55
  • Don't. Learn to live with it as it is. In fact you really should be just using [`$filter`](https://docs.mongodb.com/manual/reference/operator/aggregation/filter/) and accepting that this an array. You embedded it in the document so you should not expect to just get those results **only**. If you always want the embedded items only, then put them in a separate collection instead. Mangling documents using the aggregation pipeline like this is a performance hog to be avoided. At least `$filter` can apply with "more or less" the same performance as a regular `.find()`. – Neil Lunn Jun 18 '17 at 06:00
  • Ok thank you i will try with $filter – Shrawan Lakhe Jun 18 '17 at 06:07
  • See [Retrieve only the queried element in an object array in MongoDB collection](https://stackoverflow.com/questions/3985214/retrieve-only-the-queried-element-in-an-object-array-in-mongodb-collection) for example usage of `$filter`. – Neil Lunn Jun 18 '17 at 06:15

0 Answers0