LOGBOOK

HELP

Quiz Entry - updated: 2026.06.20

How do you sort results and select specific fields in MongoDB?

Pass an options object to find() with a sort property to order results and a projection property to include or exclude fields.

const results = await collection.find({}, {
  sort: { name: 1 },
  projection: { _id: 0, name: 1, day: 0 }
}).toArray();

Sort values:

  • 1 = ascending
  • -1 = descending

Projection values:

  • 1 = include field
  • 0 = exclude field

From Quiz: WEBT / Database Connections | Updated: Jun 20, 2026