3

I have following document:

{
    _id : 21353456,
    username : "xy",
    text : "asdf",
    comments : [
        {
            username : "User1",
            text : "hi",
        },
        {
            username : "User2",
            text : "hi1",
        },
        {
            username : "User3",
            text : "hi2",
        },
        {
            username : "User4",
            text : "hi3",
        }

    ]
}

Now I want to get the username, text and comments with aggregation and project. In addition I also want a boolean if the comments array contains an username with "User1". I have this, but it doesn't work.

db.posttest.aggregate(
   [
     {
       $project:
          {
            username: 1,
            text: 1,
            comments : 1,
            hasComment: { $eq: [ "comments.$.username", "User1" ] },
            _id: 0
          }
     }
   ]
)

2 Answers 2

8

To achieve this you would need to first unwind the comments, and then use a group with a little trick. If you want to omit the _id, then you would also need to do a simple project. Here is the full aggregation pipeline:

db.posttest.aggregate([
  { $unwind : "$comments" },
  { $group : {
    _id : "$_id",
    username : { $first : "$username" },
    text : { $first : "$text" },
    comments : { $push : "$comments" },
    hasComments : { $max : { $eq : [ "$comments.username", "User1" ] } }
  }},
  { $project : { _id : false } }
])

An explanation is following.

First, we need to get rid of an array (comments). To do this we unwind the record; it gives us four records:

{
  "_id" : 21353456,
  "username" : "xy",
  "text" : "asdf",
  "comments" : {
    "username" : "User1",
    "text" : "hi"
  }
},
{
  "_id" : 21353456,
  "username" : "xy",
  "text" : "asdf",
  "comments" : {
    "username" : "User2",
    "text" : "hi1"
  }
},
{
  "_id" : 21353456,
  "username" : "xy",
  "text" : "asdf",
  "comments" : {
    "username" : "User3",
    "text" : "hi2"
  }
},
{
  "_id" : 21353456,
  "username" : "xy",
  "text" : "asdf",
  "comments" : {
    "username" : "User4",
    "text" : "hi3"
  }
}

Now we can group all the records into one applying a function to each field. First, we need to give criteria, the 'group by' field (or set of fields). In our case, it is simply the id: _id: "$_id".

Then, for each field, we need to make a decision on how to include it into the resulting record. We have few fields: username, text, and comments. For each four records the username and text are the same, so we can easily pick any of them, namely $first or $last.

comments, however, are different. We want to preserve all of them so that we $push each one back.

The hasComments is a bit tricky here: we need to check if at least one comment.username contains the username. We can use $eq: [...] here, it will give us some array, e.g. [true, false, false, false] or [false, false, true, false]. We would need to pick which value goes into the resulting record. In this case, we can use neither $first nor $last. However, $max will give us an appropriate result.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, great answer!
1

I know this is an old question and also has an accepted answer, but there is an easiest way than using $unwind and $group, only using $project like this:

The trick here is compare the intersection beetween comments array and desired value (in this case User1. If the interesection is greater than 0 (i.e. exists the value) then the field existsUser will be true, otherwise false.

{
  "$project": {
    "comments": 1,
    "existsUser": {
      "$cond": {
        "if": {
          "$gt": [
            {
              "$size": {
                "$setIntersection": [
                  "$comments.username",
                  [
                    "User1"
                  ]
                ]
              }
            },
            0
          ]
        },
        "then": true,
        "else": false
      }
    }
  }
}

Example here

Comments

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.