w3resource

MongoDB Projection Operator - $slice

Description

In MongoDB, the $slice operator decide the retrieval of a number of items of an array that a query returns. The $slice accepts a number including a negative number as an argument and returns the output. The argument can be the form of skip and limit, that means the $slice operator retrieve the number of items specified in the limit from the array to skip a specific of a number of items.

Syntax:

db.collection.find( { field: value }, { array: {$slice: count| [skip, limit] } } );

Parameters:

Name Description
collection name of the collection.
field name of the field or column.
array name of the array types field or column.
count number of items to be retrieved from the array. If a number of counts is more than the number of items in the array, then all the items will be retrieved from the array.
skip a number of items to skip from the array.
limit number or items to be retrieved from the array.

Our database name is 'myinfo' and our collection name is 'table2'. Here, is the collection bellow.

Sample collection "table2"

{
        "_id" : ObjectId("52860b2cf313832c9a6f1018"),
        "student_id" : "STU001",
        "sem" : "sem1",
        "score" : [
                45,
                59,
                75,
                67,
                52,
                72
        ]
}
{
        "_id" : ObjectId("52860b81f313832c9a6f1019"),
        "student_id" : "STU002",
        "sem" : "sem1",
        "score" : [
                55,
                69,
                57,
                76,
                63,
                49
        ]
}

Example - 1 of MongoDB Projection Operator - $slice

If we want to select all documents from the collection "table2" which satisfying the conditions -

The student_id is "STU001"

Display the score of first 3 subjects

the following mongodb command can be used :

>db.table2.find({ "student_id":"STU001" }, { "score": { $slice: 3}}).pretty();

N.B. find() method displays the documents in a non structured format but to display the results in a formatted way, the pretty() method can be used.

Output:

{
        "_id" : ObjectId("52860b2cf313832c9a6f1018"),
        "student_id" : "STU001",
        "sem" : "sem1",
        "score" : [
                45,
                59,
                75
        ]
}

Example - 2 of MongoDB Projection Operator - $slice

If we want to select all documents from the collection "table2" which satisfying the conditions -

display the score of first 3 subjects for all students.

the following mongodb command can be used :

>db.table2.find({}, { "score": { $slice: 3}}).pretty();

N.B. find() method displays the documents in a non structured format but to display the results in a formatted way, the pretty() method can be used.

Output:

{
        "_id" : ObjectId("52860b2cf313832c9a6f1018"),
        "student_id" : "STU001",
        "sem" : "sem1",
        "score" : [
                45,
                59,
                75
        ]
}
{
        "_id" : ObjectId("52860b81f313832c9a6f1019"),
        "student_id" : "STU002",
        "sem" : "sem1",
        "score" : [
                55,
                69,
                57
        ]
}

Example - 3 of MongoDB Projection Operator - $slice

If we want to select all documents from the collection "table1" which satisfying the conditions -

Display the score of fourth and fifth subject for all the students.

the following mongodb command can be used :

>db.table2.find({}, { "score": { $slice: [3,2]}}).pretty();

N.B. find() method displays the documents in a non structured format but to display the results in a formatted way, the pretty() method can be used.

Output:

{
        "_id" : ObjectId("52860b2cf313832c9a6f1018"),
        "student_id" : "STU001",
        "sem" : "sem1",
        "score" : [
                67,
                52
        ]
}
{
        "_id" : ObjectId("52860b81f313832c9a6f1019"),
        "student_id" : "STU002",
        "sem" : "sem1",
        "score" : [
                76,
                63
        ]
}

Previous: $elemMatch (projection)
Next: Field Update Operators $inc



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/mongodb/mongodb-slice-operators.php