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

# $createObjectId (aggregation) (expression operator)

## Definition

**New in version 8.3**

Generate a new random [`ObjectId()`](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-ObjectId) value.

Use [`$createObjectId`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/createobjectid.md#mongodb-expression-exp.-createObjectId) to generate unique ObjectId values in an aggregation pipeline or expression-based update.

For example, you can generate new identifier fields or replace existing `id` values so other stages can distinguish between documents. This includes operators that rely on a stable `_id` value, like [`$graphLookup`.](https://www.mongodb.com/docs/manual/reference/operator/aggregation/graphlookup.md#mongodb-pipeline-pipe.-graphLookup)

## Syntax

[`$createObjectId`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/createobjectid.md#mongodb-expression-exp.-createObjectId) has the following syntax:

```javascript
{
   $createObjectId: { }
}
```

**Note:**

You must use an empty object (`{}`) as the argument.

## Behavior

[`$createObjectId`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/createobjectid.md#mongodb-expression-exp.-createObjectId) behaves as follows:

| Argument | Behavior |
| --- | --- |
| `{}` | Returns a new random value of BSON type [`ObjectId()`.](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-ObjectId) |
| Any other value | The operation fails with `FailedToParse`. |

**Tip:**

To convert an existing value to an ObjectId, use [`$toObjectId`.](https://www.mongodb.com/docs/manual/reference/operator/aggregation/toobjectid.md#mongodb-expression-exp.-toObjectId)

## Example

The examples on this page use data from the [sample\_mflix dataset](https://www.mongodb.com/docs/manual/sample-data/sample-mflix.md#std-label-sample-mflix). For details on how to load this dataset into your self-managed MongoDB deployment, see [Load the Sample Dataset](https://www.mongodb.com/docs/manual/sample-data/load-sample-data-local.md#std-label-sample-dataset-local). If you made any modifications to the sample databases, you may need to drop and recreate the databases to run the examples on this page.

### Generate identifiers in a view

This example adds ObjectId values to a [view](https://www.mongodb.com/docs/manual/core/views.md#std-label-views-landing-page) so other aggregation stages can rely on a stable `_id` value.

In the `sample_mflix` database, create a view over the `movies` collection that hides the original `_id` field:

```javascript
db.createView(
   "moviesView",
   "movies",
   [
      { $project: { _id: 0, title: 1, cast: 1 } }
   ]
)
```

Stages that rely on `_id` do not behave as expected with this view because the documents no longer have an `_id` field. For example, a graph traversal stage like [`$graphLookup`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/graphlookup.md#mongodb-pipeline-pipe.-graphLookup) uses `_id` internally to track visited documents and de-duplicate results.

To use this view with stages that expect a stable identifier, create a second view that adds a unique `_id` field with [`$createObjectId`:](https://www.mongodb.com/docs/manual/reference/operator/aggregation/createobjectid.md#mongodb-expression-exp.-createObjectId)

```javascript
db.createView(
   "moviesViewWithId",
   "moviesView",
   [
      {
         $project: {
            _id: { $createObjectId: {} },  // unique id
            title: 1,
            cast: 1
         }
      }
   ]
)
```

You can now run an aggregation that treats each document in `moviesViewWithId` as a distinct node. For example, the following [`$graphLookup`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/graphlookup.md#mongodb-pipeline-pipe.-graphLookup) stage finds other movies that share cast members with each movie:

```javascript
db.movies.aggregate( [
   {
      $graphLookup: {
         from: "moviesViewWithId",
         startWith: "$cast",
         connectFromField: "cast",
         connectToField: "cast",
         as: "relatedMovies"
      }
   }
] )
```

In this pipeline, [`$createObjectId`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/createobjectid.md#mongodb-expression-exp.-createObjectId) ensures that each document in the view has a unique ObjectId value in `_id`. Stages that depend on a stable identifier can then distinguish between documents correctly.
