Use $set to specify a value for a key, and if it does not exist, create it. This is useful for updating patterns or adding user definitions.
Db.user.insert ({"Name": "Codingwhy.com"})
This user now only has the name information and now needs to add an email to him
Db.user.update ({"Name": "Codingwhy.com"},{"$set": {"email": "Example@example.com"}})
{"_id": ObjectId (" 55092691591555a6c35dd698 ")," name ":" codingwhy.com "," email ":" Example@example.com "}
For example, now he has to add his personal hobbies, he may have a lot of hobbies, so hobbies should be an array
Db.user.update ({"Name": "Codingwhy.com"},{"$set": {"hobby": ["Swimming", "running", "Reading"]}})
{"_id": ObjectId ("55082691591555a6c35dd698"), "name": "codingwhy.com", "email": "example@example.com", "hobby": ["swimming", "Running", "reading"]}
If he doesn't have a hobby now ...
Db.user.update ({"Name": "Codingwhy.com"},{"$unset": {"hobby": 1}})
{"_id": ObjectId ("55082691591555a6c35dd698" ), "name": "codingwhy.com", "email": "Example@example.com"}
The hobby is gone ...