> For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

# Restore Specific Documents from Online Archive

**Important: Feature unavailable in Flex Clusters**

Flex clusters don't support this feature at this time. To learn more, see [Atlas Flex Limitations.](https://www.mongodb.com/docs/atlas/reference/flex-limitations.md#std-label-flex-cluster-limitations)

This page describes how to restore specific archived data using the [`$merge`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/merge.md#mongodb-pipeline-pipe.-merge) pipeline stage. This approach allows you execute a more granular restore based on specific criteria. If you need to restore a complete cluster, see [Back Up and Restore Archived Data](https://www.mongodb.com/docs/atlas/online-archive/config-backup-online-archive.md#std-label-oa-config-backup) for more details.

**Note:**

Ensure that your cluster is adequately provisioned for the amount of data you want to restore from your archive. Otherwise, you risk running out of space during or after the restoration. Contact [Support](https://www.mongodb.com/docs/atlas/support.md#std-label-atlas-support) for additional technical guidance on setting up the size of the oplog or for troubleshooting any space issues on your Atlas cluster.

## Required Access

You need [`Project Data Access Admin`](https://www.mongodb.com/docs/atlas/reference/user-roles.md#mongodb-authrole-Project-Data-Access-Admin) access or higher to the project to follow this procedure.

## Considerations

This approach is not recommended for large datasets (around 1TB of data) with a large number of partitions.

## Procedure

Perform the following steps to restore specific archived data to your Atlas cluster:

1. Pause the Online Archive associated with the collection that contains the archived data you want to restore.

   See [Pause and Resume Archiving](https://www.mongodb.com/docs/atlas/online-archive/pause-resume-online-archive.md#std-label-pause-resume-online-archive) for more information.

2. Connect to Online Archive using your connection string.

   You must use the Archive Only connection string to connect to the Online Archive. To learn more, see [Connect to Online Archive.](https://www.mongodb.com/docs/atlas/online-archive/connect-to-online-archive.md#std-label-connect-online-archive-atlas-ui)

3. Use [`$merge`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/merge.md#mongodb-pipeline-pipe.-merge) stage to move the data from your archive to your Atlas cluster.

   To learn more about the `$merge` pipeline stage syntax and usage for moving data back into your Atlas cluster, see the [$merge](https://www.mongodb.com/docs/atlas/data-federation/supported-unsupported/pipeline/merge.md#std-label-adf-merge-stage) pipeline stage.

   **Example:**

   Consider the following documents in an S3 (Simple Storage Service) archive:

   ```json
   {
      "_id" : 1,
      "item": "cucumber",
      "source": "nepal",
      "released": ISODate("2016-05-18T16:00:00Z")
   }
   {
      "_id" : 2,
      "item": "miso",
      "source": "canada",
      "released": ISODate("2016-05-18T16:00:00Z")
   }
   {
      "_id" : 3,
      "item": "oyster",
      "source": "luxembourg",
      "released": ISODate("2016-05-18T16:00:00Z")
   }
   {
      "_id" : 4,
      "item": "mushroom",
      "source": "ghana",
      "released": ISODate("2016-05-18T16:00:00Z")
   }
   ```

   Suppose you intend to restore documents based on the `item` and `source` fields during the `$merge` stage. The following code sample shows an example of using the [`$merge`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/merge.md#mongodb-pipeline-pipe.-merge) stage to restore archived data based on that criteria:

   ```json
   db.<collection>.aggregate([
      {
         "$merge": {
            "into": {
               "atlas": {
                  "clusterName": "<atlas-cluster-name>",
                  "db": "<db-name>",
                  "coll": "<collection-name>"
               }
            },
            "on": [ "item", "source" ],
            "whenMatched": "keepExisting",
            "whenNotMatched": "insert"
         }
      }
   ])
   ```

   The code employs the following logic:

   - If an archived document matches a document on the Atlas cluster on the `item` and `source` fields, Atlas keeps the existing document in the cluster because the copy of the document on the Atlas cluster is more recent than the archived version.

   - If an archived document doesn't match any document in the Atlas cluster, Atlas inserts the document into the specified collection on the Atlas cluster.

   When restoring data to the Atlas cluster, the archived data might have duplicate `_id` fields. For this example, we can include a [`$sort`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/sort.md#mongodb-pipeline-pipe.-sort) stage for sorting on the `_id` and `released` fields before the [`$merge`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/merge.md#mongodb-pipeline-pipe.-merge) stage to ensure that Atlas chooses the documents with the most recent date if there are duplicates to resolve.

   The following code sample adds the [`$sort`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/sort.md#mongodb-pipeline-pipe.-sort) stage:

   ```json
   db.runCommand({
      "aggregate": "<collection>", "pipeline": [
         {
            $sort: {
               "_id": 1, "released": 1,
            }
         }, {
            "$merge": {
               "into": {
                  "atlas": {
                     "clusterName": "<atlas-cluster-name>", "db": "<db-name>",
                     "coll": "<collection-name>"
                  }
               }, "on": [ "item", "source" ], "whenMatched": "keepExisting",
               "whenNotMatched": "insert"
            }
         }
      ], "cursor": { }
   }, { "background": true }
   )
   ```

   To learn more about resolving duplicate fields, see the [$merge considerations.](https://www.mongodb.com/docs/atlas/data-federation/supported-unsupported/pipeline/merge.md#std-label-adf-merge-stage)

   If there are multiple `on` fields, you must create a compound [unique index](https://www.mongodb.com/docs/manual/core/index-unique/) on the `on` identifier fields:

   ```shell
   db.<collection>.createIndex( { item: 1, source: 1 }, { unique: true } )
   ```

   Alternatively, specify merges sequentially, one for each `on` identifier field, to a temporary collection. Then, merge the data in the temporary collection to the target collection using the cluster's connection string. You must still create a unique index for each `on` identifier field.

   You can run the aggregation stage in the background by setting the `background` flag to `true`. To run this command in [`mongosh`](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh), use the `db.runCommand`.

4. Verify data in the Atlas cluster and delete the online archive.

   See [Delete an Online Archive](https://www.mongodb.com/docs/atlas/online-archive/manage-online-archive.md#std-label-delete-online-archive) for more information.

**Note:**

If you run into issues while migrating data back to your Atlas cluster, contact [Support.](https://www.mongodb.com/docs/atlas/support.md#std-label-atlas-support)
