Articles → MONGODB → $Addtoset Operator In Mongodb

$Addtoset Operator In Mongodb






Purpose





Syntax


db.collection.update(
   { [query] },
   { $addToSet: { [field]: [value] } }
)



Example




Picture showing the sample mongodb collection




db.mycollection.updateOne(
   { "_id": 1 },
   { $addToSet: { "interests": "sports" } }
)


Picture showing executing the updateOne query with $addToSet



Output


Picture showing the output of $addToSet in mongodb



Posted By  -  Karan Gupta
 
Posted On  -  Monday, April 15, 2024

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250