I have MongoDB collection items with following documents:
{ "values" : [1, 2] }
{ "values" : [5, null] }
{ "values" : [-5, null] }
Is there any way to sort this collection by values property without null values? My current query is:
db.items.aggregate([{"$sort": {"values": 1}}])
with this result:
{ "values" : [5, null] }
{ "values" : [-5, null] }
{ "values" : [1, 2] }
First values are [5, null] and [-5, null] because null is smallest value in collection. However, I would like to ignore null values and sort by numbers only, so:
{ "values" : [-5, null] }
{ "values" : [1, 2] }
{ "values" : [5, null] }
When there is no number (both values are null), document should be last in result.