You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
676 B
28 lines
676 B
# Mongo - Advanced Find
|
|
|
|
## Lesson Objectives
|
|
1. Explain Field Selection
|
|
1. Explain Ordering
|
|
1. Explain Paging
|
|
1. Explain Count
|
|
|
|
## Explain Field Selection
|
|
1. `db.employees.find({}, {name: 1});`
|
|
- 1 for include
|
|
- 0 for exclude
|
|
1. `{name:1, _id: 0}` excludes _id
|
|
|
|
## Explain Ordering
|
|
1. `db.employees.find().sort({name: 1, salary: -1})`
|
|
- 1 for ascending
|
|
- -1 for descending
|
|
- won't sort on large set without index
|
|
|
|
## Explain Paging
|
|
1. `db.employees.find().sort({weight: -1}).limit(2).skip(1)`
|
|
- can help avoid issues when sorting on large un-indexed fields
|
|
|
|
## Explain Count
|
|
1. `db.employees.find({salary: {$gt: 50}}).count()`
|
|
- usual count is just a shortcut to this
|