MongoDB and Minimongo sorting null and undefined differently

464 Views Asked by At

When sorting ascending in MongoDB, result is that null and undefined are first and then fields with value. When sorting ascending in Minimongo, result is that null is first, then fields with value and then undefined at the end. This difference is making problem. Is there a way to MongoDB and Minimongo sort the same way null and undefined? Minimongo sorting

2

There are 2 best solutions below

0
kevinadi On

This is more an issue with minimongo, since MongoDB published the canonical BSON type sort order and has been using this type ordering for a long time.

Unfortunately minimongo hasn't been updated for more than a year.

2
Seba Kerckhof On

If you execute in the browser console:

const test = new Mongo.Collection(null);
test.insert({foo: undefined});
test.insert({foo:'foo'});
test.insert({foo:null});
test.find({}, { sort: { foo: 1 } }).map(i => i.foo);

It correctly prints: [undefined, null, 'foo']. So please provide a reproduction.