sixshotsix
sixshotsix

Reputation: 25

Mongo Query That Sorts All Data Minus Null Data

I'm trying to run a query that sorts my data (data collected about airplane flights) by highest altitude recorded. When i run a sort aggregation from descending order, all entries with a "null" value for altitude are displayed first. I've tried to run use a greater than expression but I must have the syntax wrong.

db.planes.aggregate(
[
{$sort: {altitude : -1} }
],
{allowDiskUse: true}
)

This runs the data in order, but displays a large list of "null" data first. I need to not include that data.

Upvotes: 0

Views: 37

Answers (1)

felix
felix

Reputation: 9285

Just filter out document with null altitude before sorting:

db.planes.aggregate([
  {$match: {altitude: {$ne: null}}},
  {$sort: {altitude: -1} }
],
  {allowDiskUse: true}
); 

Upvotes: 1

Related Questions