0

I have the following document:

"content": [
    {
        "_id": "5dbef12ae3976a2775851bfb",
        "name": "Item AAA",
        "itemImages": [
            {                 
                "_id": "5dbef12ae3976a2775851bfd",
                "imagePath": "https://via.placeholder.com/300",
                "imageTitle": "Test 300"
            },
            {
                "_id": "5dbef12ae3976a2775851bfc",
                "imagePath": "https://via.placeholder.com/250",
                "imageTitle": "Test 250"                 
            }
        ]
    }

and I am wondering if there is a way to return only the data in the array yet with the "name" and document "main _id" so that result set will be

        "itemImages": [
            {                 
                "_id": "5dbef12ae3976a2775851bfb",
                "name": "Item AAA",                    
                "imagePath": "https://via.placeholder.com/300",
                "imageTitle": "Test 300"
            },
            {
                "_id": "5dbef12ae3976a2775851bfb",
                "name": "Item AAA",                    
                "imagePath": "https://via.placeholder.com/250",
                "imageTitle": "Test 250"                 
            }
        ]

I've tried using mongodb find and aggregate functions but neither helped in retrieving the above results. Thanks for your help

1 Answer 1

1

You should be able to get what you want with aggregation.

You'll need to:

  • unwind the array so there is a separate document for each element
  • use addFields to add the _id and name to the element
  • group to reassemble the array
  • project to get just the field you want

This might look something like:

db.collection.aggregate([
     {$unwind:"$itemImages"},
     {$addFields: {
           "itemImages._id":"$_id",
           "itemImages.name":"$name"
     }},
     {$group:{_id:"$_id", itemImages:{$push:"$itemImages"}}},
     {$project:{itemImages:1,_id:0}}
])
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Joe for your help, worked perfectly, one more question if you don't mind please. I am trying to slice the result to return images from X number of items using { $project: { itemImages: { $slice: [ '$itemImages', offset, size ] }, _id: 0 } } yet by applying the $slice command it slice the number of images returned in a single item rather than number of items returned, so was wondering what i am doing wrong / missing here? Thanks again
Remember that the $project is applied separately to each document in the stream. If you want to limit the number of documents returned, use $limit and/or $skip. Also note that if you do not include a sort, paginating with skip/limit may not return what you expect. Also, since aggregate returns a cursor, iterating using cursor methods is usually more efficient.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.