I recently upgraded from Mongoose 5.x to 8.x and one of my queries that previously worked is no longer returning any documents, and nothing in the migration guides seemed to indicate why that would be the case.
Here is a reduced/simplified example of one of the collection items from our fixture data:
{
_id: ObjectId('5ff8c2dd6b59a6000d7bfdb2'),
__t: 'Item',
name: 'Item Name',
tags: {
tag1: [
'option1',
'option2',
'option3',
],
tag2: [
'option1',
],
},
},
My query looks something like this:
{"$and":[{"$and":[{"tags.tag1":["option2"]},{"tags.tag2":["option1"]}]}
In Mongoose 5.x, the fixture document above would be returned from this query. However in 8.x, it is not. I tested a bunch of things and it turns out if I were to remove option1
and option3
such that the new tags object looked like this:
tags: {
tag1: [
'option2',
],
tag2: [
'option1',
],
},
then the item does get returned from the query. This seems to indicate that it will only be returned if the tag1 array matches the query exactly, rather than simply containing the item requested.
How would one ask Mongoose to return the document if the tag1 array includes option2
, even if there are other tags in the array?