$slice Modifier in MongoDB – Easy Guide

 In this tutorial, I will explain the $slice modifier in MongoDB.

The $slice modifier will help us limit the number of elements to insert when we conduct a $push operation. The $slice modifier must be used in conjunction with the $each operator. Only the $slice modifier has an impact if you give an empty array [] to the $each operator.

This guide will give several examples of using the $slice modifier in MongoDB. So, let us get started.

The $slice Modifier in MongoDB Syntax

Let us take a look at the syntax of the $slice modifier in MongoDB:

{
  $push: {
     field: {
       $each: [ value1, value2, ... ],
       $slice: num
     }
  }
}

Using the $slice Modifier in MongoDB

Let us begin our discussion of the various examples that I want to cover using the $slice modifier in MongoDB.

Slicing an Array Field from the End Using the $slice Modifier

  • Start up the Mongo local database server
  • Choose and navigate into a database you want to work in using the use db_name command in the mongo shell
  • Let us take a look at all the documents available to us in the drones collections in the droneStore database:
> use droneStore
switched to db droneStore

Viewing Existing Documents in the Collection

> db.drones.find({}).pretty()
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0c"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Nimbari Gryphon Medeta 65",
        "price" : 77500,
        "weight" : "77 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Delivery",
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0d"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "X-Strimmer Eye",
        "price" : 23500,
        "weight" : "24 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Land Inspection",
                        "Water Inspection"
                ]
        }
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0e"),
        "utility" : [
                "Delivery",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Khai Balemosh Shefqa TRX",
        "price" : 120500,
        "weight" : "80 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Monitoring or Inspection",
                        "Land Inspection"
                ]
        }
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0f"),
        "utility" : [
                "Photography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Sifinist Croma AX",
        "price" : 99500,
        "weight" : "97 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Videography",
                        "Water Inspection"
                ]
        }
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf10"),
        "utility" : [
                "Security",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Drovce Finnifield FR-7",
        "price" : 87600,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "polysterene",
                "moreUses" : [
                        "Land Inspection",
                        "Videography"
                ]
        }
}

Slicing Documents from the End

  • We will now use the $slice modifier to trim the utility array to the last 5 items. We will set the value as -5 because we want to trim values from the end of the array:
> db.drones.updateMany( { }, { $push : { utility : { $each : [ "Cinematography", "Range-finding Photography" ], $slice : -5 } } } )

{ "acknowledged" : true, "matchedCount" : 5, "modifiedCount" : 5 }
  • Let us now check our documents:
> db.drones.find({}).pretty()
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0c"),
        "utility" : [
                "Combat",
                "Rescue",
                "Construction",
                "Cinematography",
                "Range-finding Photography"
        ],
        "onSale" : false,
        "name" : "Nimbari Gryphon Medeta 65",
        "price" : 77500,
        "weight" : "77 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Delivery",
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0d"),
        "utility" : [
                "Combat",
                "Rescue",
                "Construction",
                "Cinematography",
                "Range-finding Photography"
        ],
        "onSale" : false,
        "name" : "X-Strimmer Eye",
        "price" : 23500,
        "weight" : "24 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Land Inspection",
                        "Water Inspection"
                ]
        }
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0e"),
        "utility" : [
                "Combat",
                "Rescue",
                "Construction",
                "Cinematography",
                "Range-finding Photography"
        ],
        "onSale" : false,
        "name" : "Khai Balemosh Shefqa TRX",
        "price" : 120500,
        "weight" : "80 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Monitoring or Inspection",
                        "Land Inspection"
                ]
        }
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0f"),
        "utility" : [
                "Combat",
                "Rescue",
                "Construction",
                "Cinematography",
                "Range-finding Photography"
        ],
        "onSale" : false,
        "name" : "Sifinist Croma AX",
        "price" : 99500,
        "weight" : "97 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Videography",
                        "Water Inspection"
                ]
        }
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf10"),
        "utility" : [
                "Combat",
                "Rescue",
                "Construction",
                "Cinematography",
                "Range-finding Photography"
        ],
        "onSale" : false,
        "name" : "Drovce Finnifield FR-7",
        "price" : 87600,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "polysterene",
                "moreUses" : [
                        "Land Inspection",
                        "Videography"
                ]
        }
}

You can see all the documents’ utility array fields have just 5 elements even though we inserted 2 elements while they already had 4.

Slicing an Array Field from the Start Using the $slice Modifier

We will now trim arrays from the start by 6 elements this time using the $slice modifier in MongoDB.

> db.drones.updateMany( { }, { $push : { utility : { $each : [ "Wildlife Monitoring", "Precision Agriculture" ], $slice : 6 } } } )

{ "acknowledged" : true, "matchedCount" : 5, "modifiedCount" : 5 }
  • Let us take a quick look at our documents now:
> db.drones.find({}).pretty()
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0c"),
        "utility" : [
                "Combat",
                "Rescue",
                "Construction",
                "Cinematography",
                "Range-finding Photography",
                "Wildlife Monitoring"
        ],
        "onSale" : false,
        "name" : "Nimbari Gryphon Medeta 65",
        "price" : 77500,
        "weight" : "77 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Delivery",
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0d"),
        "utility" : [
                "Combat",
                "Rescue",
                "Construction",
                "Cinematography",
                "Range-finding Photography",
                "Wildlife Monitoring"
        ],
        "onSale" : false,
        "name" : "X-Strimmer Eye",
        "price" : 23500,
        "weight" : "24 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Land Inspection",
                        "Water Inspection"
                ]
        }
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0e"),
        "utility" : [
                "Combat",
                "Rescue",
                "Construction",
                "Cinematography",
                "Range-finding Photography",
                "Wildlife Monitoring"
        ],
        "onSale" : false,
        "name" : "Khai Balemosh Shefqa TRX",
        "price" : 120500,
        "weight" : "80 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Monitoring or Inspection",
                        "Land Inspection"
                ]
        }
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0f"),
        "utility" : [
                "Combat",
                "Rescue",
                "Construction",
                "Cinematography",
                "Range-finding Photography",
                "Wildlife Monitoring"
        ],
        "onSale" : false,
        "name" : "Sifinist Croma AX",
        "price" : 99500,
        "weight" : "97 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Videography",
                        "Water Inspection"
                ]
        }
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf10"),
        "utility" : [
                "Combat",
                "Rescue",
                "Construction",
                "Cinematography",
                "Range-finding Photography",
                "Wildlife Monitoring"
        ],
        "onSale" : false,
        "name" : "Drovce Finnifield FR-7",
        "price" : 87600,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "polysterene",
                "moreUses" : [
                        "Land Inspection",
                        "Videography"
                ]
        }
}

See Also:

Conclusion

Learn to use the $slice operator in MongoDB to trim array fields inside the mongo shell.

Noteworthy References

MongoDB Docs

Aneesha S
Aneesha S
Articles: 171