09 - Perform New Updates Using model.findOneAndUpdate - MongoDB and Mongoose - freeCodeCamp Tutorial
Ganesh H
Mongoose has some methods findOneAndUpdate() and findByIdAndUpdate() that allow us to update records in one step. We pass in a query object to find our desired record, a second object with our desired properties, a third object with an option to return the new record, and finally a callback function.
Link to challenge : https://www.freecodecamp.org/learn/apis-and-microservices/mongodb-and-mongoose/perform-new-updates-on-a-document-using-model-findoneandupdate Written Guide : https://www.notion.so/ganeshh123/Perform-New-Updates-on-a-Document-Using-model-findOneAndUpdate-f3fe6ef3689d4eaaae1e875336d37e12
Full Playlist for this course : https://www.youtube.com/playlist?list=PLhGp6N0DI_1QJMb63oecYZ3lLjrF4PnIs All Writen Guides for this course : https://www.notion.so/ganeshh123/6f987e7260914612b06adf840dbb00c3 All My Tutorials can be found at : https://www.notion.so/Tutorials-Ganesh-H-293ea420d34a464f9a1907e0405b5f26
Concepts: model.findOneAndUpdate() Issues a mongodb findAndModify update command. Finds a matching document, updates it according to the update arg, passing any options, and returns the found document (if any) to the callback. The query executes if callback is passed else a Query object is returned. https://mongoosejs.com/docs/api/model.html#model_Model.findOneAndUpdate
model.findByIdAndUpdate() Issues a mongodb findAndModify update command by a document's _id field. findByIdAndUpdate(id, ...) is equivalent to findOneAndUpdate({ _id: id }, ...). Finds a matching document, updates it according to the update arg, passing any options, and returns the found document (if any) to the callback. The query executes if callback is passed. https://mongoosejs.com/docs/api/model.html#model_Model.findByIdAndUpdate
Model Models are fancy constructors compiled from Schema definitions. An instance of a model is called a document. Models are responsible for creating and reading documents from the underlying MongoDB database. https://mongoosejs.com/docs/models.html
-————————————————————————————————————- MongoDB is a cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with optional schemas. MongoDB is developed by MongoDB Inc. and licensed under the Server Side Public License (SSPL).
Mongoose is an Object Data Modeling (ODM) library for MongoDB and Node.js. It manages relationships between data, provides schema validation, and is used to translate between objects in code and the representation of those objects in MongoDB.
freeCodeCamp (also referred to as “Free Code Camp”) is a non-profit organization that consists of an interactive learning web platform, an online community forum, chat rooms, online publ ... https://www.youtube.com/watch?v=1UnZyC25pt4
28911199 Bytes