
- 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
How to find latest entries in array over all MongoDB documents?
To find latest entries in array over all document, use aggregate(). Let us create a collection with documents −
> db.demo179.insertOne( ...{ ... "Name":"Chris", ... "Details": [ ... { ... "Id":101, ... "Subject":"MongoDB" ... }, ... { ... "Id":102, ... "Subject":"MySQL" ... } ... ] ...} ...); { "acknowledged" : true, "insertedId" : ObjectId("5e3980299e4f06af551997f9") } > db.demo179.insertOne( ...{ ... "Name":"David", ... "Details": [ ... { ... "Id":103, ... "Subject":"Java" ... }, ... { ... "Id":104, ... "Subject":"C" ... } ... ] ...} ...); { "acknowledged" : true, "insertedId" : ObjectId("5e39802a9e4f06af551997fa") }
Display all documents from a collection with the help of find() method −
> db.demo179.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e3980299e4f06af551997f9"), "Name" : "Chris", "Details" : [ { "Id" : 101, "Subject" : "MongoDB" }, { "Id" : 102, "Subject" : "MySQL" } ] } { "_id" : ObjectId("5e39802a9e4f06af551997fa"), "Name" : "David", "Details" : [ { "Id" : 103, "Subject" : "Java" }, { "Id" : 104, "Subject" : "C" } ] }
Following is the query to find latest entries in array over all documents −
> db.demo179.aggregate([ ... { "$unwind": "$Details" }, ... { "$sort": { "Details.Id": -1 } }, ... { "$limit": 2 }, ... { ... "$group": { ... "_id": "$Details.Id", ... "Name" : { "$first": "$Name" }, ... "Details": { "$push": "$Details" } ... } ... }, ... { ... "$project": { ... "_id": 0, "Name": 1, "Details": 1 ... } ... } ...])
This will produce the following output −
{ "Name" : "David", "Details" : [ { "Id" : 103, "Subject" : "Java" } ] } { "Name" : "David", "Details" : [ { "Id" : 104, "Subject" : "C" } ] }
- Related Articles
- How to find documents with exactly the same array entries as in a MongoDB query?
- Find MongoDB documents where all objects in array have specific value?
- How to update all documents in MongoDB?
- How to aggregate array documents in MongoDB?
- How to iterate over all MongoDB databases?
- Count unique items in array-based fields across all MongoDB documents?
- Efficient way to remove all entries from MongoDB?
- Find documents where all elements of an array have a specific value in MongoDB?\n
- Find in MongoDB documents with filled nested array and reshape the documents result
- How to search documents through an integer array in MongoDB?
- How to filter documents based on an array in MongoDB?
- How to delete all the documents from a collection in MongoDB?
- Find all documents that have two specific id's in an array of objects in MongoDB?
- MongoDB query to find matching documents given an array with values?
- How do I find all documents with a field that is NaN in MongoDB?

Advertisements