Updating Documents

Explore different update operators ($set, $inc, $push, $pull, etc.) and methods to modify existing documents in a collection.


MongoDB Essentials: The $push Operator

Understanding the $push Operator

The $push operator in MongoDB is used to append one or more values to an array field within a document. It's a powerful tool for managing lists of data stored directly within your documents.

Essentially, if the field already exists as an array, $push adds the specified value(s) to the end of that array. If the field doesn't exist, $push creates a new array field and initializes it with the specified value(s).

How to Use the $push Operator

Let's explore how to use $push with examples. Assume we have a collection called products with documents like this:

 {
            "_id": ObjectId("6432a5b1c8d8c90001c2a5c3"),
            "name": "Awesome Gadget",
            "tags": ["electronics", "new"]
        } 

Now, let's add a new tag, "featured", to the "tags" array using $push:

 db.products.updateOne(
            { "name": "Awesome Gadget" },
            { $push: { "tags": "featured" } }
        ) 

After running this update, the document will look like this:

 {
            "_id": ObjectId("6432a5b1c8d8c90001c2a5c3"),
            "name": "Awesome Gadget",
            "tags": ["electronics", "new", "featured"]
        } 

Pushing Multiple Values

You can also push multiple values at once using the $each modifier:

 db.products.updateOne(
            { "name": "Awesome Gadget" },
            { $push: { "tags": { $each: ["popular", "discounted"] } } }
        ) 

The document will now look like this:

 {
            "_id": ObjectId("6432a5b1c8d8c90001c2a5c3"),
            "name": "Awesome Gadget",
            "tags": ["electronics", "new", "featured", "popular", "discounted"]
        } 

$push on a Non-Existent Array

If the array field doesn't exist, $push will create it:

 db.products.updateOne(
            { "name": "Awesome Gadget" },
            { $push: { "ratings": 5 } }
        ) 

Now the document will have a 'ratings' field:

 {
            "_id": ObjectId("6432a5b1c8d8c90001c2a5c3"),
            "name": "Awesome Gadget",
            "tags": ["electronics", "new", "featured", "popular", "discounted"],
            "ratings": [5]
        } 

Using $slice with $push to Limit Array Size

The $slice modifier can be used with $push to limit the maximum size of the array. For instance, if you want to maintain the last 5 ratings:

 db.products.updateOne(
            { "name": "Awesome Gadget" },
            {
                $push: {
                    "ratings": {
                        $each: [4, 3, 5],
                        $slice: -5  // Keep only the last 5 elements
                    }
                }
            }
        ) 

In summary, the $push operator is a versatile tool for managing array data in MongoDB documents. By understanding how to use it with modifiers like $each and $slice, you can effectively handle lists of data within your documents.