- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to use $slice operator to get last element of array in MongoDB?
To get the last element of array in MongoDB, use the following syntax
db.yourCollectionName.find({},{yourArrayFieldName:{$slice:-1}});
Let us first create a collection with documents
>db.getLastElementOfArrayDemo.insertOne({"StudentName":"James","StudentMathScore":[78,68,98]}); { "acknowledged" : true, "insertedId" : ObjectId("5c9d2d71a629b87623db1b2e") } >db.getLastElementOfArrayDemo.insertOne({"StudentName":"Chris","StudentMathScore":[88,56,34]}); { "acknowledged" : true, "insertedId" : ObjectId("5c9d2d83a629b87623db1b2f") } >db.getLastElementOfArrayDemo.insertOne({"StudentName":"Larry","StudentMathScore":[99]}); { "acknowledged" : true, "insertedId" : ObjectId("5c9d2d8ea629b87623db1b30") } >db.getLastElementOfArrayDemo.insertOne({"StudentName":"Robert","StudentMathScore":[90,78,67,66,75,73]}); { "acknowledged" : true, "insertedId" : ObjectId("5c9d2dada629b87623db1b31") }
Following is the query to display all documents from a collection with the help of find() method
> db.getLastElementOfArrayDemo.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5c9d2d71a629b87623db1b2e"), "StudentName" : "James", "StudentMathScore" : [ 78, 68, 98 ] } { "_id" : ObjectId("5c9d2d83a629b87623db1b2f"), "StudentName" : "Chris", "StudentMathScore" : [ 88, 56, 34 ] } { "_id" : ObjectId("5c9d2d8ea629b87623db1b30"), "StudentName" : "Larry", "StudentMathScore" : [ 99 ] } { "_id" : ObjectId("5c9d2dada629b87623db1b31"), "StudentName" : "Robert", "StudentMathScore" : [ 90, 78, 67, 66, 75, 73 ] }
Following is the query to get the last element of array in MongoDB
> db.getLastElementOfArrayDemo.find({},{StudentMathScore:{$slice:-1}});
This will produce the following output
{ "_id" : ObjectId("5c9d2d71a629b87623db1b2e"), "StudentName" : "James", "StudentMathScore" : [ 98 ] } { "_id" : ObjectId("5c9d2d83a629b87623db1b2f"), "StudentName" : "Chris", "StudentMathScore" : [ 34 ] } { "_id" : ObjectId("5c9d2d8ea629b87623db1b30"), "StudentName" : "Larry", "StudentMathScore" : [ 99 ] } { "_id" : ObjectId("5c9d2dada629b87623db1b31"), "StudentName" : "Robert", "StudentMathScore" : [ 73 ] }
- Related Articles
- MongoDB query to slice only one element of array
- MongoDB aggregate $slice to get the length of the array
- Aggregate a $slice to get an element in exact position from a nested array in MongoDB?
- How to get the last element in JavaScript array?
- MongoDB Aggregation to slice array inside array
- How to get a particular element from MongoDB array?
- How to Get a slice of a primitive array in Java?
- MongoDB query to get average in aggregation of array element?
- How to get last element in android ConcurrentLinkedDeque?
- MongoDB slice array in populated field?
- C# program to get the last element from an array
- Python How to get the last element of list
- How to get the last N records in MongoDB?
- How to get the size of an array or a slice in Golang?
- How to get the index of an array element in older versions on MongoDB?

Advertisements