How do I use array containing objects as parameter in queries?

Hi, I’m going to ask you for help with a rather different situation. I’m not sure such a query is possible. I hope there is a solution.

db

{

  users: [

    {

      _id: 1,

      userid: "user1",

      title: "title 1",

      content: "content 1",

      createdAt: 08

    },

    {

      _id: 2,

      userid: "user2",

      title: "title 2",

      content: "content 2",

      createdAt: 10

    },

    {

      _id: 3,

      userid: "user2",

      title: "title 3",

      content: "content 3",

      createdAt: 12

    },

    {

      _id: 4,

      userid: "user3",

      title: "title 4",

      content: "content 4",

      createdAt: 13

    }

  ]

}

(second and third post belong to the same user)

I want to use the following array as parameter.

[{ userid: "user1", lastAccessDate: 07, }, { userid: "user2", lastAccessDate: 11, }]

1- I want to list the posts of the users in the array.
2- But I want to list the posts whose “createdAt” value is greater than “lastAccessDate” value in the same index.

My English is not enough. I hope I was able to explain.
According to the example and conditions I gave, posts with id 1 and 3 will be listed.

Please help me how to do this.

Sorry if I was rude. Have a nice day

Hello @MUSTAFA_SAHIN, you can try this aggregation query (runs from mongo shell).

var INPUT_ARRAY = [ 
    { userid: "user1", lastAccessDate: 07, }, 
    { userid: "user2", lastAccessDate: 11, } 
]

db.users.aggregate([
  { 
    $group: { 
        _id: "$userid", 
        docs: { $push: "$$ROOT" } 
    } 
  },
  { 
    $addFields: { 
        docs: {
            $filter: { 
                 input: "$docs",
                 as: "doc",
                 cond: { $gt: [
                     { $let: { 
                          vars: { 
                              filtered:  { 
                                  $filter: { 
                                      input: INPUT_ARRAY,
                                      as: "inarr",
                                      cond: { $and: [ { $gt: [ "$$doc.createdAt", "$$inarr.lastAccessDate" ] },
                                                      { $eq: [ "$$doc.userid", "$$inarr.userid" ] }
                                      ] }
                                  } 
                              }
                          },
                          in: {  $size: "$$filtered"  }
                     }
                 }, 0 ] }
            }
        }
    }
  },
  { 
    $unwind: "$docs" 
  },
  { 
    $replaceWith: "$docs" 
  }
])

The query returns an output as:

{
        "_id" : 3,
        "userid" : "user2",
        "title" : "title 3",
        "content" : "content 3",
        "createdAt" : 12
}
{
        "_id" : 1,
        "userid" : "user1",
        "title" : "title 1",
        "content" : "content 1",
        "createdAt" : 8
}