Docs Menu
Docs Home
/ / /
Scala
/

Specify Fields To Return

On this page

  • Overview
  • Sample Data
  • Projection Types
  • Specify Fields to Include
  • Exclude the _id Field
  • Specify Fields to Exclude
  • Additional Information
  • API Documentation

In this guide, you can learn how to use the Scala driver to specify which fields to return from a read operation by using a projection. A projection is a document that specifies which fields MongoDB returns from a query.

The examples in this guide use the restaurants collection in the sample_restaurants database from the Atlas sample datasets. To access this collection from your Scala application, create a MongoClient that connects to an Atlas cluster and assign the following values to your database and collection variables:

val database: MongoDatabase = client.getDatabase("sample_restaurants")
val collection: MongoCollection[Document] = database.getCollection("restaurants")

To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see the Get Started with Atlas guide.

You can use a projection to specify which fields to include in a return document, or to specify which fields to exclude. You cannot combine inclusion and exclusion statements in a single projection, unless you are excluding the _id field.

To specify the fields to include in the result, chain the projection() method to the find() method. The Projections class provides the include() helper method that you can use to set the fields to include.

The following example uses the find() method to find all restaurants in which the name field value is "Emerald Pub". Then, the code calls the projection() and include() methods to instruct the find operation to return only the name, cuisine, and borough fields of matching documents:

collection
.find(equal("name", "Emerald Pub"))
.projection(include("name", "cuisine", "borough"))
.subscribe((doc: Document) => println(doc.toJson()),
(e: Throwable) => println(s"There was an error: $e"))
{"_id": {"$oid": "..."}, "borough": "Manhattan", "cuisine": "American", "name": "Emerald Pub"}
{"_id": {"$oid": "..."}, "borough": "Queens", "cuisine": "American", "name": "Emerald Pub"}

When you use a projection to specify fields to include in the return document, the _id field is also included by default. All other fields are implicitly excluded. To remove the _id field from the return document, you must explicitly exclude it.

When specifying fields to include, you can also exclude the _id field from the returned document. The Projections class provides the excludeId() helper method that you can use to omit this field.

The following example performs the same query as the preceding example but excludes the _id field from the projection:

collection
.find(equal("name", "Emerald Pub"))
.projection(fields(include("name", "cuisine", "borough"), excludeId()))
.subscribe((doc: Document) => println(doc.toJson()),
(e: Throwable) => println(s"There was an error: $e"))
{"borough": "Manhattan", "cuisine": "American", "name": "Emerald Pub"}
{"borough": "Queens", "cuisine": "American", "name": "Emerald Pub"}

To specify the fields to exclude from the result, chain the projection() method to the find() method. The Projections class provides the exclude() helper method that you can use to set the fields to exclude.

The following example uses the find() method to find all restaurants in which the name field value is "Emerald Pub". Then, the code calls the projection() and exclude() methods to instruct the find operation to omit the name and address fields in the result:

collection
.find(equal("name", "Emerald Pub"))
.projection(exclude("name", "address"))
.subscribe((doc: Document) => println(doc.toJson()),
(e: Throwable) => println(s"There was an error: $e"))
{"_id": {"$oid": "..."}, "borough": "Manhattan", "cuisine": "American",
"grades": [...], "restaurant_id": "40367329"}
{"_id": {"$oid": "..."}, "borough": "Queens", "cuisine": "American",
"grades": [...], "restaurant_id": "40668598"}

When you use a projection to specify which fields to exclude, any unspecified fields are implicitly included in the return document.

To learn more about projections, see the Project Fields guide in the MongoDB Server manual.

To learn more about any of the methods or types discussed in this guide, see the following API documentation:

Back

Specify Documents to Return