Answer the question
In order to leave comments, you need to log in
How to compose a mongodb query with an empty array?
Actually I try to make request on two fields. One of the fields receives an empty array. Finds nothing. How to do what would find? Or make this field optional?
var shops = [123, 345, 567];
var types = [];
{
'shops': {
$in: shops
},
'types': {
$in: types
}
}
Answer the question
In order to leave comments, you need to log in
Build the query dynamically
var shops = [123, 345, 567];
var types = [];
var query = {};
if(shops.length) {
query.shops = { $in: shops };
}
if(types.length) {
query.types = { $in: types };
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question