Fundamentos de Bases de Datos NoSQL
NoSQL: El Otro Tipo de Bases de Datos
¿Qué debo elegir? NoSQL vs SQL
Manipulación de Datos en MongoDB
Tus primeros pasos con MongoDB
Creación de Documentos en MongoDB
Uso de la Consola de MongoDB: Creación de Datos con insertOne e insertMany
Eliminar Documentos en MongoDB
Cómo Leer Documentos en MongoDB con find()
Consultas Avanzadas en MongoDB: Dominando el Framework de Agregación
Cómo Eliminar Datos en MongoDB
Operaciones avanzadas de reemplazo en MongoDB
Cómo Actualizar Documentos en MongoDB
Tipos de Bases de Datos NoSQL
Bases de Datos de Grafos: Conceptos y Aplicaciones Prácticas
Bases de Datos de Grafos: Ejercicios y Casos de Uso
Introducción a las Bases de Datos basadas en Documentos
Introducción a las Bases de Datos Clave-Valor
Introducción a las Bases de Datos Vectoriales
Pasos Futuros
Alcances y Beneficios de NoSQL
You don't have access to this class
Keep learning! Join and start boosting your career
Modifying or updating documents in MongoDB is essential for handling dynamic data. Although there are several methods to perform these actions, the most common are update
and replace
. Each is used in different scenarios and it is crucial to understand their differences and applications.
update
used in MongoDB?update
?The update
method allows you to modify certain values of a document that meet a specific filter. There are variants such as updateOne
and updateMany
.
updateOne
?To update a single document in MongoDB, updateOne
is used. This method requires a filter to identify the document and the $set
operator to specify the changes. For example, to change the name of a customer:
db.customers.updateOne( { _id: ObjectId("5f3e5a3a3a29f1e8e8b7c2c69d62") }, { $set: { name: "Elizabeth" } } );
This command searches for the document with the specified _id
and updates the name
field.
updateMany
?To update multiple documents that meet a criterion, updateMany
is used. This method also requires a filter and the changes to be made:
{ db.customers.updateMany( { birthYear: { $gte: 1990 } }, { $set: { membership: { "Platinum" } } );
In this example, all documents where birthYear
is greater than or equal to 1990 will be updated to include the membership
field with the value Platinum
.
replace
in MongoDB?replaceOne
work?The replaceOne
method replaces an entire document except for its identifier. This is useful when you need to restructure a document:
db.customers.replaceOne( { _id: ObjectId("5f3e5a3a3a29f1e8e8b7c2c69d62") }, { name: "John Doe", age: 30, city: "New York" } );
This command replaces the document identified by _id
with a new one that has the name
, age
and city
fields.
update
and replace
?In addition to update
and replace
, MongoDB offers other methods for data manipulation, such as bulkWrite
, which allows multiple operations to be performed in a single call, and findAndModify
, which returns and modifies documents in a single step.
For further practice, try the following challenge: in the Airbnb database, update all apartments with less than three rooms by subtracting 10 from the price. This will help you apply the concepts you have learned.
db.airbnb.updateMany( { bedrooms: { $lt: 3 } }, { $inc: { price: -10 } } );
Contributions 6
Questions 0
Want to see more contributions, questions and answers from the community?