Tutorials Exercises Get Certified Services Menu
Log in
Sign Up Spaces Bootcamps

MongoDB Aggregation $out

Aggregation $out

This aggregation stage writes the returned documents from the aggregation pipeline to a collection.

The $out stage must be the last stage of the aggregation pipeline.


In this example, we are using the "sample_airbnb" database loaded from our sample data in the Intro to Aggregations section.

    $group: {
      _id: "$property_type",
      properties: {
        $push: {
          name: "$name",
          accommodates: "$accommodates",
          price: "$price",
  { $out: "properties_by_type" },
Try it Yourself »

The first stage will group properties by the property_type and include the name, accommodates, and price fields for each. The $out stage will create a new collection called properties_by_type in the current database and write the resulting documents into that collection.