Interface FindOneAndReplaceOptions

Represents the options for the findOneAndReplace command.

Field

returnDocument - Specifies whether to return the original or updated document.

Field

upsert - If true, perform an insert if no documents match the filter.

Field

sort - The sort order to pick which document to replace if the filter selects multiple documents.

Field

projection - Specifies which fields should be included/excluded in the returned documents.

Field

includeResultMetadata - When true, returns alongside the document, an ok field with a value of 1 if the command executed successfully.

Field

maxTimeMS - The maximum time to wait for a response from the server, in milliseconds.

See

Collection.findOneAndReplace

interface FindOneAndReplaceOptions {
    includeResultMetadata?: boolean;
    maxTimeMS?: number;
    projection?: Projection;
    returnDocument?: "before" | "after";
    sort?: Sort;
    upsert?: boolean;
    vector?: number[];
    vectorize?: string;
}

Hierarchy (view full)

Properties

includeResultMetadata?: boolean

When true, returns alongside the document, an ok field with a value of 1 if the command executed successfully.

Otherwise, returns the document result directly.

Defaults to false.

Default Value

false
maxTimeMS?: number

The maximum time to wait for a response from the server, in milliseconds.

projection?: Projection

Specifies which fields should be included/excluded in the returned documents.

If not specified, all fields are included.

When specifying a projection, it's the user's responsibility to handle the return type carefully, as the projection will, of course, affect the shape of the returned documents. It may be a good idea to cast the returned documents into a type that reflects the projection to avoid runtime errors.

Example

interface User {
  name: string;
  age: number;
}

const collection = db.collection<User>('users');

const doc = await collection.findOne({}, {
  projection: {
  _id: 0,
  name: 1,
  },
  vector: [.12, .52, .32],
  includeSimilarity: true,
}) as { name: string, $similarity: number };

// Ok
console.log(doc.name);
console.log(doc.$similarity);

// Causes type error
console.log(doc._id);
console.log(doc.age);
returnDocument?: "before" | "after"

Specifies whether to return the document before or after the update.

Set to before to return the document before the update to see the original state of the document.

Set to after to return the document after the update to see the updated state of the document immediately.

Defaults to 'before'.

Default Value

'before'
sort?: Sort

The order in which to apply the update if the filter selects multiple documents.

If multiple documents match the filter, only one will be updated.

Defaults to null, where the order is not guaranteed.

Default Value

null
upsert?: boolean

If true, perform an insert if no documents match the filter.

If false, do not insert if no documents match the filter.

Defaults to false.

Default Value

false
vector?: number[]

An optional vector to use of the appropriate dimensionality to perform an ANN vector search on the collection to find the closest matching document.

This is purely for the user's convenience and intuitiveness—it is equivalent to setting the $vector field in the sort field itself. The two are interchangeable, but mutually exclusive.

If the sort field is already set, an error will be thrown. If you really need to use both, you can set the $vector field in the sort object directly.

Deprecated

  • Prefer to use sort: { $vector: [...] } instead
vectorize?: string

Akin to FindOneAndReplaceOptions.vector, but for $vectorize.

Deprecated

  • Prefer to use sort: { $vectorize: '...' } instead