MongoDB is a cross-platform document-oriented and a non relational (i.e NoSQL) database program. It is an open-source document database, that stores the data in the form of key-value pairs.
First create a database on which we perform the update_one() operation:
It is a function by which we can update a record in a MongoDB database or Collection. This method mainly focuses on two arguments that we passed one is the query (i.e filter) object defining which document to update and the second is an object defining the new values of the document(i.e new_values) and the rest arguments are optional that we will discuss in the syntax section. This function finds the first document that matches with the query and update it with an object defining the new values of the document, i.e Updates a single document within the collection based on the filter.
collection.update_one(filter, new_values, upsert=False, bypass_document_validation=False, collation=None, array_filters=None, session=None)
- ‘filter’ : A query that matches the document to update.
- ‘new_values’ : The modifications to apply.
- ‘upsert’ (optional): If “True”, perform an insert if no documents match the filter.
- ‘bypass_document_validation’ (optional) : If “True”, allows the write to opt-out of document level validation. Default is “False”.
- ‘collation’ (optional) : An instance of class: ‘~pymongo.collation.Collation’. This option is only supported on MongoDB 3.4 and above.
- ‘array_filters’ (optional) : A list of filters specifying which array elements an update should apply. Requires MongoDB 3.6+.
- ‘session’ (optional) : a class:’~pymongo.client_session.ClientSession’.
Example 1: In this example, we are going to update the fan quantity from 10 to 25.
Example 2: In this example we are changing the tv company name from ‘samsung’ to ‘sony’ by using update_one():
NOTE :The “$set” operator replaces the value of a field with the specified value. If the field does not exist, “$set” will add a new field with the specified value, provided that the new field does not violate a type constraint.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- MongoDB and Python
- MongoDB Python | Insert and Update Data
- MongoDB python | insert(), replace_one(), replace_many()
- MongoDB python | Delete Data and Drop Collection
- Python MongoDB - Find
- Python MongoDB - Sort
- Create a database in MongoDB using Python
- 3D Plotting sample Data from MongoDB Atlas Using Python
- Geospatial Queries with Python MongoDB
- Python Mongodb - Delete_one()
- Python Mongodb - Delete_many()
- How to fetch data from MongoDB using Python?
- How to access a collection in MongoDB using Python?
- Python MongoDB - insert_many Query
- Drop Collection if already exists in MongoDB using Python
- Python MongoDB - Update_many Query
- Python MongoDB - insert_one Query
- Python MongoDB - find_one Query
- How to create index for MongoDB Collection using Python?
- Python MongoDB - create_index Query
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.