w3resource
MongoDB Tutorial

MongoDB Aggregation Stages Operator - $out

Description

The MongoDB $out write the resulting document of the aggregation pipeline to a specified collection. The $out operator must be the last stage in the pipeline. The $out operator lets the aggregation framework return result sets of any size.

Syntax:

{ $out: "<output-collection>" }

Properties:

Create New Collection

The $out operation creates a new collection in the current database if the specified one does not already exist. Until the aggregation completes the collection can not be visible. The new collection can not be created by MongoDB if the aggregation faild.

Replace Existing Collection

If the specified collection already exists, the $out stage atomically replaces the existing collection with the new results collection. This operation does not change any indexes that are already exists in the collection. If the aggregation fails, the $out operation makes no changes to the pre-existing collection.

Index Constraints

The pipeline will fail to complete if the produced document by the pipeline would violate any unique indexes, including the index on the _id field of the original output collection.

Sample collection invoice

{
        "_id" : 1,
        "item" : "doz",
        "qty" : 20,
        "rate" : 10,
        "inv_date" : "02/02/2014"
}
{
        "_id" : 2,
        "item" : "sam",
        "qty" : 15,
        "rate" : 8,
        "inv_date" : "05/12/2014"
}
{
        "_id" : 3,
        "item" : "amp",
        "qty" : 25,
        "rate" : 8,
        "inv_date" : "07/02/2014"
}
{
        "_id" : 4,
        "item" : "doz",
        "qty" : 20,
        "rate" : 10,
        "inv_date" : "02/02/2014"
}
{
        "_id" : 5,
        "item" : "amp",
        "qty" : 10,
        "rate" : 8,
        "inv_date" : "05/12/2014"
}
{
        "_id" : 6,
        "item" : "doz",
        "qty" : 30,
        "rate" : 10,
        "inv_date" : "13/04/2014"
}
{
        "_id" : 7,
        "item" : "sam",
        "qty" : 15,
        "rate" : 8,
        "inv_date" : "05/12/2014"
}
{
        "_id" : null,
        "item" : "mks",
        "qty" : 10,
        "rate" : 20,
        "inv_date" : "17/12/2014"
}

Example : $out

The following aggregation operation returns the data of the invoice collection to have invoice date grouped by the item and then writes the results to the newinvoice collection.

db.invoice.aggregate( [
                      { $group : { _id : "$item", invoiceDate: { $push: "$inv_date" } } },
                      { $out : "newinvoice" }
                  ] );

After the operation, the newinvoice collection contains the following documents:

> db.newinvoice.find().pretty();
{ "_id" : "mks", "invoiceDate" : [ "17/12/2014" ] }
{ "_id" : "amp", "invoiceDate" : [ "07/02/2014", "05/12/2014" ] }
{ "_id" : "sam", "invoiceDate" : [ "05/12/2014", "05/12/2014" ] }
{
        "_id" : "doz",
        "invoiceDate" : [
                "02/02/2014",
                "02/02/2014",
                "13/04/2014"
        ]
}

Previous: $group
Next: $setEquals



New Content: Composer: Dependency manager for PHP, R Programming