How to realize that if the data exists, update if it does not exist, delete it in mongo


 description

Update the data whose name is cmdhelp in the test table, if it does not exist, create it

 command

db.test.update(
   { "name": "cmdhelp" },
   { "$set": { "age":1} },
   { upsert: true }
)

 parameter

update :  modify an existing document or a document in a collection
$set :  Append new fields to existing documents
upsert :  If no document matches the query, create a new document

 result

Updated 1 new record(s) in 17ms

I hope to help every developer quickly find the commands they need. Of course, you can also add your own commonly used commands for easy searching.