Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add blog post on MongoDB sorting for scalar values #3200

Merged
merged 33 commits into from
Oct 31, 2023
Merged
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
318dce9
init
chilagrow Aug 15, 2023
3551b17
add content about BSON type and comparison order
chilagrow Aug 15, 2023
c2c220d
Merge branch 'main' into blog-post-sorting
chilagrow Aug 15, 2023
b7ec89d
add examples
chilagrow Aug 15, 2023
5fdf229
add explanations to examples
chilagrow Aug 15, 2023
00d70b0
update heading
chilagrow Aug 16, 2023
5548bcc
use official image
chilagrow Aug 16, 2023
00bd8e6
expand example for using deterministic sorting
chilagrow Aug 16, 2023
dc24593
minor formatting
chilagrow Aug 16, 2023
3581094
docs-fmt
chilagrow Aug 16, 2023
e92f0d6
Merge branch 'main' into blog-post-sorting
chilagrow Aug 16, 2023
e0ffd64
fix inconsistent usage of Bson type
chilagrow Aug 16, 2023
76886a3
Merge branch 'main' into blog-post-sorting
chilagrow Aug 28, 2023
97036d3
address comments
chilagrow Aug 28, 2023
18bdcc9
rewording
chilagrow Aug 28, 2023
8611f6d
minor fix
chilagrow Aug 28, 2023
9827dc2
merge conflict
chilagrow Oct 23, 2023
6e05c02
cleanup
chilagrow Oct 23, 2023
097fb56
rename
chilagrow Oct 25, 2023
0d383e2
update keyword
chilagrow Oct 25, 2023
66ea256
fix bad merge
chilagrow Oct 25, 2023
95b08e5
update linkedin url
chilagrow Oct 25, 2023
c9e8903
cleanup
chilagrow Oct 25, 2023
9de8349
use one image for the blog
chilagrow Oct 25, 2023
4312f66
remove keywords
chilagrow Oct 25, 2023
6611e51
Format blog
Fashander Oct 30, 2023
2d6a07c
Edit blog
Fashander Oct 30, 2023
1793b9e
Update website/blog/2023-10-25-mongodb-sorting-scalar.md
Fashander Oct 30, 2023
5f2e0a7
Update website/blog/2023-10-25-mongodb-sorting-scalar.md
Fashander Oct 30, 2023
993c4bf
Merge branch 'main' into blog-post-sorting
chilagrow Oct 30, 2023
e6d0171
update date of blog
chilagrow Oct 31, 2023
99de106
Merge branch 'main' into blog-post-sorting
AlekSi Oct 31, 2023
418d3e3
Update website/blog/2023-10-31-mongodb-sorting-scalar.md
AlekSi Oct 31, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
cleanup
  • Loading branch information
chilagrow committed Oct 23, 2023
commit 6e05c02dabc2489a30268e377a658304a866b178
12 changes: 6 additions & 6 deletions website/blog/2023-08-14-mongodb-sorting-scalar.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,19 @@ For example, Null BSON type has the lowest order of comparison 1, so Null is les
Boolean BSON type on the other hand has the order of comparison 8, a BSON type such as ObjectId with a lower order of comparison is less than a Boolean value
and a BSON type such as Timestamp with a higher order of comparison is greater than a Boolean value.

Comparing different BSON types is merely looking up the order of comparison table for each BSON type and comparing the predefined order.
Comparing different BSON types is merely looking up the order of comparison table for each BSON type.
There is an exception for Array values, we will be discussing them in another blog post.

### Number comparison

Although numbers have different BSON types, namely Integer, Long, Double and Decimal, they are considered the equivalent BSON type for the purpose of comparison.
That means comparing numbers consider their values but whether they are Integer, Long, Double or Decimal are not relevant.
For instance, an Integer value 0 is equivalent to Double 0.0 as far as comparison is concerned.
For instance, an Integer value 0 is equivalent to Double 0.0.

### Null and non-existent field comparison

For the comparison purpose, a non-existent field is equivalent to Null.
This means that a field `v` with Null value `{:v null}` and a non-existent `v` field in `{}` are equal as far as comparison is concerned.
This means that a field `v` with Null value `{:v null}` and a non-existent `v` field in `{}` are equivalent.

## Examples showcasing sorting for scalar values

Expand Down Expand Up @@ -101,11 +101,11 @@ db.outfits.find().sort({ size: 1 })
The output is sorted and the first document is `slippers` which is missing the `size` field.
A [non-existent field is equivalent to Null](#null-and-non-existent-field-comparison), so it has the lowest BSON type.

Then the next documents are Numbers.
The numbers have higher BSON order of comparison than Null BSON type, so they come after `slippers` of the missing field.
Then the next documents have `size` field value of Numbers.
The Numbers have higher BSON order of comparison than Null BSON type, so they come after `slippers` of the missing field.
The documents with numbers are `boots` with Integer BSON type, `sneaker` with Double BSON type and `sandals` with Integer BSON type.
Notice that Integer BSON type is followed by Double BSON type then by another Integer BSON type?
The numbers are considered [equivalent BSON types](#number-comparison) so only the values of each number are compared regardless of its specific BSON number type.
The Numbers are considered [equivalent BSON types](#number-comparison) so only the values of each number are compared regardless of its specific BSON number type.
The document `boots` has a `size` field value of 8 which is less than 8.5 of `sneakers` or 9 of `sandals`, so `boots` comes after that.
Then the document `sneakers` comes next.

Expand Down
Loading