mongodbHow to use MongoDB push?
MongoDB push is a method used to add new elements to an array field in a MongoDB document. It is used to add elements to the end of an array.
Example code
db.collection.update(
{ _id: 1 },
{ $push: { array_field: "new_element" } }
)
Output example
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Code explanation
db.collection.update
: This is the MongoDB command used to update a document in a collection.{ _id: 1 }
: This is the filter used to identify the document to be updated.{ $push: { array_field: "new_element" } }
: This is the update operator used to add a new element to the end of an array field.WriteResult
: This is the output of the command, indicating that the document was successfully updated.
Helpful links
More of Mongodb
- How to use watch in MongoDB?
- How to check the version of MongoDB?
- How to use triggers in MongoDB?
- How to work with time series data in MongoDB?
- How to use unwind in MongoDB?
- How to list MongoDB users?
- How to update many documents in MongoDB?
- How to create a many to many relation in MongoDB?
- How to perform a health check for MongoDB?
- How to query with "not in" condition in MongoDB?
See more codes...