HOWTO · MongoDB

How to Select Single or Multiple Fields for All Documents in a MongoDB Collection

In this MongoDB tutorial article, you will learn how to select single or multiple fields for all documents given in a MongoDB collection by performing basic query operations through projection which specifies or restrict fields to return.

On this page

To select fields from MongoDB documents, pass a projection document as the second argument to find(). Use 1 for each field to include and, when needed, _id: 0 to omit the identifier that MongoDB includes by default.

Select Fields in a MongoDB Collection with a Projection

A projection controls which fields MongoDB returns for each matching document. The first argument to find() is the filter; the second is the projection. An empty filter, {}, matches every document in the collection.

For a hands-on example, connect mongosh to a MongoDB server and insert a small set of documents into the teams collection. These commands use the literal field names team, position, and points; they are syntax examples and are not executed here:

db.teams.insertMany([{ team: "Manchester City", position: "1st", points: 70 }, { team: "Liverpool", position: "2nd", points: 69 }, { team: "Chelsea", position: "3rd", points: 59 }]) Running this insert again adds another set of documents, so use a fresh collection if you want to repeat the example.

To inspect all fields in the inserted documents, run db.teams.find({}). The shell displays the matching documents, including their generated _id values. Representative result (illustrative; ObjectId values vary): { _id: ObjectId("<generated id>"), team: "Manchester City", position: "1st", points: 70 }, { _id: ObjectId("<generated id>"), team: "Liverpool", position: "2nd", points: 69 }, { _id: ObjectId("<generated id>"), team: "Chelsea", position: "3rd", points: 59 }. The examples below use the same collection and return fields from those documents.

Include Specific Fields and Exclude _id

Set a field to 1 in the projection to include it. For example, db.teams.find({}, { team: 1 }) returns the team field for each matching document and also includes _id by default. Representative result (illustrative; ObjectId values vary): { _id: ObjectId("<generated id>"), team: "Manchester City" }, { _id: ObjectId("<generated id>"), team: "Liverpool" }, { _id: ObjectId("<generated id>"), team: "Chelsea" }. The projection does not rename the field: its database identifier remains team.

To return team without the identifier, add _id: 0: db.teams.find({}, { team: 1, _id: 0 }). Representative result (illustrative): { team: "Manchester City" }, { team: "Liverpool" }, { team: "Chelsea" }. MongoDB allows _id to be excluded while including other fields. This is the exception to the general projection rule: a projection cannot mix inclusion and exclusion for ordinary fields. For example, do not combine team: 1 with points: 0; instead, list the fields to include.

To include more than one field and omit _id, use db.teams.find({}, { team: 1, points: 1, _id: 0 }). Each returned document contains team and points. Representative result (illustrative): { team: "Manchester City", points: 70 }, { team: "Liverpool", points: 69 }, { team: "Chelsea", points: 59 }. To include the identifier with these fields, leave _id out of the projection or set _id: 1.

The find() query returns a cursor of matching documents, which mongosh displays interactively. A projection changes the fields in each returned document; it does not limit how many documents match. Add filter criteria as the first argument when you want to restrict which documents are returned as well as choose their fields.

Summary

Pass a projection as the second argument to find(): use 1 to include one or more fields. MongoDB includes _id by default, so add _id: 0 when you want to omit it. Inclusion projections can exclude _id, but otherwise keep a projection in inclusion mode or exclusion mode.