title: Querying objects for an array of attributes in Mongoose
body: <p>I am trying to fetch chats for an array of users.
  Chats schemas are defined like this:</p>
  <pre><code>const ChatSchema = new Schema&lt;IChatSchema&gt;(
    {
      messages: [
        {
          type: Schema.Types.ObjectId,
          ref: &quot;MessageSchema&quot;,
        },
      ],
      participants: [
        {
          type:Schema.Types.ObjectId,
          ref: &quot;UserSchema&quot;,
        }
      ]
    },
    {
      timestamps: true,
    }
  );
  </code></pre>
  <p>I have two usersnames 'A' and 'B' and I want to query common chats of those two users. Any idea how to do it?</p>
  <p>User schema</p>
  <pre><code>const UserSchema = new Schema&lt;IUserSchema&gt;(
    {
      username: {
        type: String,
        required: true,
        unique:true,
      },
    },
    {
      timestamps: true,
    }
  );
  </code></pre>
  <p>I tried this approach but did not work.</p>
  <pre><code>let chat = await Chats.find({
      participants: { $elemMatch: { username: usernames } },
    })
  </code></pre>
  <p>I also tried this</p>
  <pre><code>let chat = await Chats.find({
      &quot;participants.username&quot;: { $all: usernames },
    })
  </code></pre>