Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query distinct not null values in MongoDb

Tags:

mongodb

I'm just beginning to learn MongoDb and am having some problems with distinct queries.

If I for instance run the query

db.images.distinct('gallery') 

I get the expected result, but also empty strings and null values. How can I write a query that just returns the values that are not null?

Thanks

like image 558
hfogel Avatar asked Dec 01 '22 19:12

hfogel


1 Answers

to avoid only null values use $ne

db.images.distinct( "gallery" , { "gallery" : { $ne : null } } );

or avoid "",null and more by specifying in an array using $nin.

db.images.distinct( "gallery" , { "gallery" : { $nin : ["", null] } });
like image 126
Mithun Satheesh Avatar answered Dec 24 '22 01:12

Mithun Satheesh