1

I'm trying to find all documents where an array's size is equal to a field's value. For example:

This document should be found:

{
  arr: ["one", "two", "three"],
  expected: 3
}

But not this one:

{
  arr: ["one", "two", "three", "four"],
  expected: 2
}

I'm assuming I have to use some form of aggregation, so I've decided to use $expr:

$expr: { $eq: [{ $size: "$arr" }, { ??? }] }
Ashh
  • 44,693
  • 14
  • 105
  • 132
APixel Visuals
  • 1,508
  • 4
  • 20
  • 38

1 Answers1

1

Similarly as you used the arr field using $ sign, you have to use for expected field

db.collection.find({ "$expr": { "$eq": [{ "$size": "$arr" }, "$expected"] })
Ashh
  • 44,693
  • 14
  • 105
  • 132