Mongodb find data with empty array


#1

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.

Cheers!


#2

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


#3

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


#4

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


#5

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