Filter out an object with arrays having specific ids on the basis of an existing collection?

I’m having two objects,

const originTimeStamp = {
    chats: '2021-06-25T12:21:21.835+00:00',
    users: '2021-06-21T12:21:21.835+00:00',
    history: '2021-06-18T12:21:21.835+00:00'
}

const controlIds = {
    chats: ['1bfe','2bfs','3bhr'],
    users: ['6jkj'],
    history: ['8her'],
}

and a collection that typically have some logs related to user activities:

{
  controlId: '2bfs'
  createdAt: '2021-07-19T12:21:21.835+00:00'
},
{
  controlId: '6jkj'
  createdAt: '2021-06-18T12:21:21.835+00:00'
},
{
  controlId: '8her'
  createdAt: '2021-06-25T12:21:21.835+00:00'
},

What I basically want to do is to filter out the controlIds object in such a way that if the control id exists in the collection and If the origin time stamp of that section say for chats ‘2021-06-25T12:21:21.835+00:00’ < ‘2021-07-19T12:21:21.835+00:00’ (of id ‘2bfs’ from collection ) we will remove that id from the object.

Expected Result:

const controlIds = {
    chats: ['1bfe','3bhr'],
    users: ['6jkj'],
    history: [],
}

Is there any way to achieve it with aggregation pipeline, right now i tried creating a flow but not able to do that?

I’m not sure this is what you look for @Ashish_Bairwa. I’d follow this procedure:

  1. $match the documents with a particular range of timestamps
  2. $project only the control_Id
  3. $group them using $push to get all documents in an array
  4. Assign this output to a variable and filter your object.
2 Likes