ANNOUNCEMENT: Voyage AI joins MongoDB to power more accurate and trustworthy AI applications on Atlas.
Learn more
Menu Docs

Bulk.find.updateOne()

Dica

O MongoDB também fornece o método db.collection.bulkWrite() para executar operações de gravação em massa.

Bulk.find.updateOne(<update>)

Adds a single document update operation to a bulk operations list.

Use the Bulk.find() method to specify the condition that determines which document to update. The Bulk.find.updateOne() method limits the update to a single document. To update multiple documents, see Bulk.find.update().

Bulk.find.updateOne() aceita o seguinte parâmetro:

Parâmetro
Tipo
Descrição

documento ou pipeline

As modificações a serem aplicadas. Podem ser uma dos seguintes:

A replacement document

Contains only field and value pairs.

Consulte também Bulk.find.replaceOne().

Atualizar documento

Pipeline de agregação

Contém apenas os seguintes estágios de agregação:

For more information on the update modification parameter, see the db.collection.updateOne() reference page.

The sum of the associated <query> document from the Bulk.find() and the update document must be less than or equal to the maximum BSON document size.

Esse comando está disponível em implantações hospedadas nos seguintes ambientes:

  • MongoDB Atlas: o serviço totalmente gerenciado para implantações do MongoDB na nuvem

Observação

Este comando é aceito em todos os clusters do MongoDB Atlas. Para obter informações sobre o suporte do Atlas a todos os comandos, consulte Comandos não suportados.

If the <update> document contains only update operator expressions, as in:

{
$set: { status: "D" },
$inc: { points: 2 }
}

Then, Bulk.find.updateOne() updates only the corresponding fields, status and points, in the document.

O exemplo a seguir inicializa um Bulk() construtor de operações para a items coleção e adiciona várias updateOne() operações à lista de operações.

var bulk = db.items.initializeUnorderedBulkOp();
bulk.find( { status: "D" } ).updateOne( { $set: { status: "I", points: "0" } } );
bulk.execute();

Os métodos de atualização podem aceitar um pipeline de agregação. Por exemplo, os seguintes usos:

  • o estágio $set, que pode fornecer um comportamento semelhante à expressão do operador de atualização $set,

  • the aggregation variable NOW, which resolves to the current datetime and can provide similar behavior to a $currentDate update operator expression. To access aggregation variables, prefix the variable with double dollar signs $$ and enclose in quotes.

var bulk = db.items.initializeUnorderedBulkOp();
bulk.find( { status: "P" } ).updateOne(
[
{ $set: { points: 0, lastModified: "$$NOW" } }
]
);
bulk.execute();

Veja também: