javascriptmongodbmongoosemongodb-query

updateMany setting field to size of filter condition with not in array


I have a document like this:

{
  id: string,
  subDocCount: number,
  subDocs: [
    {
      id: string (not an ObjectId),
      dateField: Date,
      booleanField: boolean
    }
  ]
}

I want to set the subDocCount field to the count of subDocs where:

I have tried with this updateMany query but have not been able to figure out the "not in array" part (mongoosejs by the way):

  await docs.updateMany(
    {},
    [
      {
        $set: {
          subDocCount: {
            $size: {
              $filter: {
                input: '$subDocs',
                as: 'subDoc',
                cond: {
                  $and: [
                    { $eq: ['$$subDoc.booleanField', false] },
                    {
                      $or: [
                        { $eq: ['$$subDoc.dateField', null] },
                        { $lt: ['$$subDoc.dateField', startOfToday()] }
                      ]
                    },
                    // this part doesn't work, but doesn't cause an error
                    { $not: { $in: ["$$subDoc.id", excludedIds] } }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  )

Am I able to somehow use a "not in array" type of clause in the filter here?

EDIT

The issue seemed to be with the matching clause I was using in the code, which was omitted here in the example as I thought it was not the issue. I was matching using a string id instead of an ObjectId. I thought this was not the issue because it seems these string ids work when querying through various methods.


Solution

  • The issue seemed to be with the matching clause I was using in the code, which was omitted here in the example as I thought it was not the issue. I was matching using a string id instead of an ObjectId. I thought this was not the issue because it seems these string ids work when querying through various methods.