title: How to query for more than one item in an array in Firestore? ("array-contains")
body: <p>I am building a 1-to-1 chat application with Firestore and Javascript (vue.js).</p>
  
  <p>I have a conversation and a message document defined as follows:</p>
  
  <p><strong>.collection("coversation").doc()</strong></p>
  
  <pre><code>conversation: {
    participantsArray: ["id1", "id2"],
    participants: {
      id1: {
        /* details like avatar &amp; name */
      },
      id2: {
        /* details like avatar &amp; name */
      }
    }
  }
  </code></pre>
  
  <p><strong>.collection("coversation").doc("foo").collection("messages").doc()</strong></p>
  
  <pre><code>message: {
    message: "foo"
  }
  </code></pre>
  
  <p>This is great, so I can now query for all conversations of a specific user like so:</p>
  
  <pre><code>db.collection('conversations').where("participantsArray", "array-contains", userId)
  </code></pre>
  
  <p><strong><em>The issue I am having now is the following:</em></strong></p>
  
  <p>I want to query for a specific conversation, like so:</p>
  
  <pre><code>db.collection('conversations')
    .where("participantsArray", "array-contains", userId)
    .where("participantsArray", "array-contains", chatpartnerId)
  </code></pre>
  
  <p>But this does not work since double "array-contains" is not allowed and throws an error message.</p>
  
  <p>What works for me for now is to make two calls like so:</p>
  
  <pre><code>db.collection('conversations')
    .where(`participantsArray`, '==', [userId, chatpartnerId])
  
  db.collection('conversations')
    .where(`participantsArray`, '==', [chatpartnerId, userId])
  </code></pre>
  
  <p>and then check if any of these queries returns a conversation. If so, I use that conversation to add a message, otherwise I create a new conversation.</p>
  
  <p>It works, but it's a bunch of code and quite inefficient.</p>
  
  <p>So my question would be:</p>
  
  <ol>
  <li>Is there a better way to do this?</li>
  <li>How to do "array-contains" to check for multiple items?</li>
  <li>I have both a <em>participantsArray</em> and a <em>participants</em> field because I did not figure out how to do "array-contains" on an array of objects. Is there a way or should I keep both fields?</li>
  </ol>
  
  <p><strong>PS:</strong>
  Before, I used to query on the objects in the participants object like so:</p>
  
  <pre><code>db.collection("conversations").where(`participants.${userId}.id`, "==", userId)
  </code></pre>
  
  <p>This was the proposed way to query before "array-contains" existed - but when combined with orderBy I would have to create an index for each user, which is not really an option.</p>