Get unique array values records with atleast one value exist

I have messenging platform in my app and I’m saving each message in the following format

{
   userIds: [ 'user1Id', 'user2Id' ],
   msg: "message",
   sentBy: 'user1',
   sentAt: new Date()
}


{
   userIds: [ 'user1Id', 'user3Id' ],
   msg: "message",
   sentBy: 'user1',
   sentAt: new Date()
}


{
   userIds: [ 'user1Id', 'user2Id' ],
   msg: "message",
   sentBy: 'user1',
   sentAt: new Date()
}

Is there any way to get unique conversations from the collection?

for example from above list I want to get

  {
       userIds: [ 'user1Id', 'user2Id' ],
       msg: "message",
       sentBy: 'user1',
       sentAt: new Date()
    }


    {
       userIds: [ 'user1Id', 'user3Id' ],
       msg: "message",
       sentBy: 'user1',
       sentAt: new Date()
    }

these two records,

What can be the mongo query here?

Is there any other way than querying all the records and do unique manually?

or anyone suggests better schema, I just started with this feature so I’m in a position to change schema.

I consider using like

{
   userIds: [ 'user1Id', 'user2Id' ],
   msgs: [
    {
       msg: "message",
       sentBy: 'user1',
       sentAt: new Date()
    },
    {
       msg: "message",
       sentBy: 'user2',
       sentAt: new Date()
    }
  ],
   modifiedAt: new Date()
}

but decided against it because whenever there is new msg added to msgs array the whole field will be sent to client so using the first schema.

Any suggestions appreaciated. Thanks.

have you tried this?

{ $or: [ 
 {userIds: {$in:[ 'user1Id']},
 {userIds: {$in:[ 'user2Id'}}
] }