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
minor fix
  • Loading branch information
chilagrow committed Aug 28, 2023
commit 8611f6da5aebb0591f9496f19e3ddeed7553e0c8
14 changes: 7 additions & 7 deletions website/blog/2023-08-14-mongodb-sorting-scalar.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ For instance, an Integer value 0 is equivalent to Double 0.0 as far as compariso

### Null and non-existent field comparison

For the comparison purpose, non-existent field is equivalent to Null.
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.

## Examples showcasing Sorting for scalar values
## Examples showcasing sorting for scalar values

Let's create an `outfits` collection using following query to insert documents.
Let's create an `outfits` collection using the following query to insert documents.

```js
db.outfits.insertMany([
Expand Down Expand Up @@ -106,7 +106,7 @@ The numbers have higher BSON order of comparison than Null BSON type, so they co
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 document `boots` has `size` field value of 8 which is less than 8.5 of `sneakers` or 9 of `sneakers`, so it comes next.
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.

Finally, `flip flops` with String BSON type which has a higher BSON order of comparison than Numbers comes last.
Expand Down Expand Up @@ -137,8 +137,8 @@ Null and non-existent field is considered equivalent so either of them can be th
In such a scenario, the default order that results were found from the database is used.

To consistently preserve the same sorting order, it is recommended to use `_id` as the second field of sorting order.
Such case, if `color` field has an equivalent value, it uses `_id` field to sort them, allowing consistent output.
The uniqueness property of `_id` field makes the sorting output consistent.
Such case, if the `color` field has an equivalent value, it uses the `_id` field to sort them, allowing consistent output.
The uniqueness property of the `_id` field makes the sorting output consistent.

```js
db.outfits.find().sort({ color: 1, _id: 1 })
Expand All @@ -154,7 +154,7 @@ db.outfits.find().sort({ color: 1, _id: 1 })
]
```

The output shows that `sandals` is sorted before `slippers` even though they have equivalent `color` field value.
The output shows that the document `sandals` is sorted before `slippers` even though they have equivalent `color` field value.
It is because it uses the second sort field `_id` and `sandals` has a lower `_id`.
Similarly, `flip flops` and `sneakers` have the same `color` field value.
But `flip flops` is sorted before because it has a lower `_id`.
Expand Down
Loading