0

At a certain point of my pipeline, I have a structure like this :

[
    {
        _id: 1,
        elems1: [{sub : {date: xxx}}, {sub: {date: yyy}}],
        elems2: [{sub: {date: zzz}}, {sub: {date: qqq}}]
    },
    ...
]

Which comes from a grouping :

{$group: {
    _id: '$user.id',
    elems1: {
        $push: {sub: '$other'}
        },
    elems2: {
        $push: {sub: '$other'}
        }
    }
}}

Now I want to delete the groups that have an element in elems1 OR elems2 with sub.date less than a certain date, how could I do this ?

1 Answer 1

1

When you do your $group, you can also track the "minElemDate" and then $match by that in the next pipeline stage.

$group: {
  _id: "$user.id",
  elem1s: ...,
  elem2s: ...
  minElemDate: {
    $min: {
      $cond: [
        { $or: [condition1, condition2] },
       "$user.date",
        null
      ]
    }
  }
},
{ $match: {$gte: ["$minElemDate", minDate]} }
Sign up to request clarification or add additional context in comments.

6 Comments

I like this idea, can you please elaborate on what to put in condition1 and condition2 ? That's the part where I'm confused
{$eq: ['$user.name', 'L']} and {$eq: ['$user.name', 'L']} -- it seems like those are the elements that you care about
I will remove this part, maybe it is making it confusing. I care only about the date... I will update my question
tldr of my answer -- when you do the group, also track the min date. don't try and filter it out afterwards because it'll be harder
The problem is that I don't know what to put in the $min block, since I want the min of the object date inside the array
|

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.