1
  {
    $lookup: {
      from: "site",
      "let": {
        "pid": "$data"
      },
      "pipeline": [
        {
          "$match": {
            "$expr": {
              "$in": [
                "$doc_id",
                "$$pid"
              ]
            }
          }
        }
      ],
      "as": "subComment"
    }
  },

https://mongoplayground.net/p/6t2uVwLzW0A

How to make a $lookup inside array using mongodb I did try but didn't get success countRecord should be inside data whose object_id does match.

output should be

[
  {
    "_id": ObjectId("5ebb74ef92033b3dc79aca13"),
    "content": "Here is new content",

    "data": [
      {
        "_id": ObjectId("5e7cd2019b1c522b15cca6fe"),
        "height": 1000,
        "url": "xyz.jpg",
        "width": 1000,
        "countRecord": 2,
      },
      {
        "_id": ObjectId("5e7cd2019b1c522b15cca6fd"),
        "height": 1000,
        "url": "verr.jpg",
        "width": 601
      }
    ],
    "subComment": [],
    "timestamp": 1.58934347177e+12,
    "type": "post"
  }
]

2 Answers 2

1

There are many ways you can do this, here is an easy example by changing input structure using $map

db.setting.aggregate([
    {
        $lookup: {
            from: "site",
            "let": {
                "pid": {
                    "$map": {
                        "input": "$data",
                        "as": "datum",
                        "in": "$$datum._id"
                    }
                }
            },
            "pipeline": [
                {
                    "$match": {
                        "$expr": {
                            "$in": [
                                "$doc_id",
                                "$$pid"
                            ]
                        }
                    }
                }
            ],
            "as": "subComment"
        }
    },
    {
        $addFields: {
            "data": {
                "$map": {
                    "input": "$data",
                    "as": "datum",
                    "in": {
                        "$mergeObjects": [
                            "$$datum",
                            {
                                "countRecord": {
                                    "$size": {
                                        "$filter": {
                                            "input": "$subComment",
                                            "as": "comment",
                                            "cond": {
                                                "$eq": [
                                                    "$$comment.doc_id",
                                                    "$$datum._id"
                                                ]
                                            }
                                        }
                                    }
                                }
                            }
                        ]
                    }
                }
            }
        }
    }
])
Sign up to request clarification or add additional context in comments.

4 Comments

Yes but "countRecord": 2 should come under data object
Just project in the order you want
It looks good but output should be in "data": [ { "_id": ObjectId("5e7cd2019b1c522b15cca6fe"), "height": 1000, "url": "xyz.jpg", "width": 1000, "countRecord": 2, },
Ah i understand, i changed the last stage to match what you want.
1

What I would suggest to make it easier to lookup, is to unwind the array first.

db.setting.aggregate([
  { $unwind: '$data' },                                                        // unwind the data array so that we can process it 1 by one
  {
    $lookup: {                                                                // will lookup for each to the unwinded elements
      from: 'site',
      localField: 'data._id',
      foreignField: 'doc_id',
      as: 'subComment'
    }
  },
  { $addFields: { 'data.countRecord': { $size: '$subComment' } } },           // add the count of records
  {
    $group: {                                                                 // We will use $group to revert unwinded data back to an array
      _id: {
        _id: '$_id',
        content: '$content',
        type: '$type',
        timestamp: '$timestamp'
      },
      data: { $push: '$data' }
    }
  },
  { $replaceRoot: { newRoot: { $mergeObjects: ['$_id', { data: '$data' }] } } } // restructures the objects to what you need it to look like
]);

1 Comment

Your answer is useful @zishone :thumps

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.