For AI agents: a documentation index is available at https://www.mongodb.com/pt-br/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Make the MongoDB docs better! We value your opinion. Share your feedback for a chance to win $100.
MongoDB Branding Shape
Click here >
Docs Menu

MongoDB\UpdateResult::getUpsertedId()

MongoDB\UpdateResult::getUpsertedId()

Return the ID (i.e. _id field value) of the upserted document.

function getUpsertedId(): mixed|null

The ID (i.e. _id field value) of the upserted document. If no document was upserted, null will be returned.

If the document had an ID prior to upserting (i.e. the server did not need to generate an ID), this will contain its _id field value. Any server-generated ID will be a MongoDB\BSON\ObjectId instance.

MongoDB\Exception\BadMethodCallException if this method is called and the write operation used an unacknowledged write concern.