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 use unwind in MongoDB?
- How to rename a field in MongoDB?
- How to insert new document into MongoDB?
- How to remove a field from MongoDB?
- How to use triggers in MongoDB?
- How to do text search in MongoDB?
- How to use transactions in MongoDB?
- How to find by id in MongoDB?
- How to check the version of MongoDB?
See more codes...