Mongodb find data with empty array


Hi there,

I’ve got a find query for mongodb which is working perfectly but only when it has content.

This works:

const filters = [
      {prop1: { $in : ['string1'] }},
      {prop2: { $in : ['string2'] }}

But this doesn’t:

const filters = [
      {prop1: { $in : ['string1'] }},
      {prop2: { $in : [] }}

This filters is dynamically created and I’d like to know either if I need to do something else when my arrays (the contents to find are created with other variables) are empties or if I can change the query.



Why not just push the filters to the array if they exist?


Yes, it’s my current solution but I didn’t know (I’m noob with mongo) if it was another solution in the query itself with mongo. That’s is just an example but my filter has too many properties to add and it’s not too clean doing it in that way


I agree it may be a bit clunky but I also tire quickly of seeing MongoError: $in needs an array, haha.


Another bad point about that is using Meteor (and also Angular) by default when I create an object some data properties like length or __proto__ are created on my object and I have to remove them