title: MongoDB aggregation how to provide array of ids and then return documents where an array of Ids has at least one Id matching
body: <p>I'm using MongoDB Realm and have a function that I give an array of string IDs.</p>
  <p>Using the aggregation pipeline, I then want to return all documents from a collection, where their array of ObjectIds called 'categoryIds' contains at least one of those string IDs.</p>
  <pre><code>   if (categories) {
      pipeline.push(
        {
          $project: {
            categoryObjectIds: { $map: { input: categories, as: 'category', in: { &quot;$toObjectId&quot;: &quot;$$category&quot; } } }
          }
        },
        {
          $match: {
            &quot;categoryIds&quot;: {
              &quot;$in&quot;: '$categoryObjectIds'
            }
          }
        }
      );
    }
  </code></pre>
  <p>I'm currently trying to say, if the array of strings has been provided (categories), push these stages to the end of the pipeline.</p>
  <p>The first stage is to convert the array of strings into an array of ObjectIds, the second is to try and match the documents who's categoryIds include at least one of those Ids from the first array.</p>
  <p>When I run this I get &quot;(BadValue) $in needs an array&quot;, what am I doing wrong here?</p>
  <p>UPDATE</p>
  <pre><code>{
      &quot;_id&quot;: {
          &quot;$oid&quot;: &quot;5faff3f5a87269ccfb8fd502&quot;
      },
      &quot;name&quot;: &quot;Albert's Schloss&quot;,
      &quot;categoryIds&quot;: [{
          &quot;$oid&quot;: &quot;5faff41da87269ccfb8fd503&quot;
      }, {
          &quot;$oid&quot;: &quot;5faff42ca87269ccfb8fd504&quot;
      }],
      &quot;aggregateRating&quot;: {
          &quot;$numberDecimal&quot;: &quot;4.8&quot;
      },
      &quot;photoURL&quot;: &quot;&quot;,
      &quot;location&quot;: {
          &quot;type&quot;: &quot;Point&quot;,
          &quot;coordinates&quot;: [{
              &quot;$numberDecimal&quot;: &quot;53.478214&quot;
          }, {
              &quot;$numberDecimal&quot;: &quot;-2.247828&quot;
          }]
      },
      &quot;openingTimes&quot;: {
          &quot;monday&quot;: {
              &quot;openingTime&quot;: {
                  &quot;$date&quot;: &quot;0001-01-01T09:00:00.000Z&quot;
              },
              &quot;closingTime&quot;: {
                  &quot;$date&quot;: &quot;0001-01-01T23:00:00.000Z&quot;
              }
          },
          &quot;tuesday&quot;: {
              &quot;openingTime&quot;: {
                  &quot;$date&quot;: &quot;0001-01-01T09:00:00.000Z&quot;
              },
              &quot;closingTime&quot;: {
                  &quot;$date&quot;: &quot;0001-01-01T23:00:00.000Z&quot;
              }
          },
          &quot;wednesday&quot;: {
              &quot;openingTime&quot;: {
                  &quot;$date&quot;: &quot;0001-01-01T09:00:00.000Z&quot;
              },
              &quot;closingTime&quot;: {
                  &quot;$date&quot;: &quot;0001-01-01T23:00:00.000Z&quot;
              }
          },
          &quot;thursday&quot;: {
              &quot;openingTime&quot;: {
                  &quot;$date&quot;: &quot;0001-01-01T09:00:00.000Z&quot;
              },
              &quot;closingTime&quot;: {
                  &quot;$date&quot;: &quot;0001-01-01T23:00:00.000Z&quot;
              }
          },
          &quot;friday&quot;: {
              &quot;openingTime&quot;: {
                  &quot;$date&quot;: &quot;0001-01-01T09:00:00.000Z&quot;
              },
              &quot;closingTime&quot;: {
                  &quot;$date&quot;: &quot;0001-01-01T23:00:00.000Z&quot;
              }
          },
          &quot;saturday&quot;: {
              &quot;openingTime&quot;: {
                  &quot;$date&quot;: &quot;0001-01-01T09:00:00.000Z&quot;
              },
              &quot;closingTime&quot;: {
                  &quot;$date&quot;: &quot;0001-01-01T23:00:00.000Z&quot;
              }
          },
          &quot;sunday&quot;: {
              &quot;openingTime&quot;: {
                  &quot;$date&quot;: &quot;0001-01-01T09:00:00.000Z&quot;
              },
              &quot;closingTime&quot;: {
                  &quot;$date&quot;: &quot;0001-01-01T23:00:00.000Z&quot;
              }
          }
      }
  }
  </code></pre>
  <p>This is an example document, if for example I provide the function with <code>categories = ['5faff41da87269ccfb8fd503'];</code> I would expect this document to return in the result because its categoryIds array contains one of the Ids that I provided</p>