1

Query 1 : { field: { $in: [null, 'something-1', 'someting-2', []] } }

Query 2 :

{
    $or: [
        { field: { exists: false } },
        { field: { $in: ['something-1', 'something-2'] } },
        { field: { $size: 0 } }
    ]
}

Query 2 is hard to maintain imo & hence I tried Query 1 which works as expected.

Assumptions where I am going to use this :
field will always be an array string no matter what, hence it will not include null or [] as a element of it. Also field is not indexed (Although If possible, then I would like to know whether query 1 & query 2 will have different if field was meant to use index)

I want to know if it is good to use Query 1 over Query 2 based on assumptions that I have for field

I tried searching documentation for this, but I didn't found any mention for this way of querying (Query 1), hence I have doubt for this approach

1
  • 1
    They have 2 different behaviours, see playground1 and playground2. Your assumption may have handled null and empty array case, but you may want to clarify on the case that the field does not exist
    – ray
    Commented Jun 19 at 14:23

0

Browse other questions tagged or ask your own question.