
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
Query on the last object of an array with MongoDB
To query on the last object of an array, use aggregate(). Let us create a collection with documents −
> db.demo103.insertOne( { "Details" : [ { "StudentId" : 101, "Details" : "MongoDB" }, {"StudentId" : 102, "Details" : "MySQL" }, { "StudentId" : 103, "Details" : "Java" } ], "Details1" : [ { "StudentId" : 104, "Number" : 3 } ] } ); { "acknowledged" : true, "insertedId" : ObjectId("5e2ed2dd9fd5fd66da21446e") }
Display all documents from a collection with the help of find() method −
> db.demo103.find();
This will produce the following output −
{ "_id" : ObjectId("5e2ed2dd9fd5fd66da21446e"), "Details" : [ { "StudentId" : 101, "Details" : "MongoDB" }, { "StudentId" : 102, "Details" : "MySQL" }, { "StudentId" : 103, "Details" : "Java" } ], "Details1" : [ { "StudentId" : 104, "Number" : 3 } ] }
Here is how you can query to on the last object of an array −
> db.demo103.aggregate([ { $project: { Details1: 1, Details: {$arrayElemAt: ["$Details", -1]} } }, { $match: {"Details.StudentId": 103} } ])
This will produce the following output −
{ "_id" : ObjectId("5e2ed2dd9fd5fd66da21446e"), "Details1" : [ { "StudentId" : 104, "Number" : 3 } ], "Details" : { "StudentId" : 103, "Details" : "Java" } }
- Related Articles
- MongoDB query to access an object in an array
- MongoDB query for Partial Object in an array
- MongoDB query to find last object in collection?
- Filter query on array of embedded document with MongoDB?
- MongoDB query to find data from an array inside an object?
- MongoDB query to remove empty objects in an object-array?
- Increment value of an array element with array object in MongoDB
- Query a nested field within an array with MongoDB
- Create array with MongoDB query?
- Querying from part of object in an array with MongoDB
- Query array of nested string with MongoDB?
- How do you perform an AND query on an array in MongoDB?
- MongoDB query to insert an array element with a condition?
- MongoDB query with $all in array
- MongoDB query to count the size of an array distinctly?

Advertisements