
- 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 get a particular element from MongoDB array?
You can use the aggregate framework to get a particular element from the MongoDB array. To understand the concept, let us create a collection with the document. The query to create a collection with the document is as follows −
> db.getParticularElement.insertOne({"InstructorName":"Larry","InstructorTechnicalSubject":["Java","C","C++","MongoDB","MySQL","SQL Server"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c7ee027559dd2396bcfbfb1") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.getParticularElement.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c7ee027559dd2396bcfbfb1"), "InstructorName" : "Larry", "InstructorTechnicalSubject" : [ "Java", "C", "C++", "MongoDB", "MySQL", "SQL Server" ] }
Here is the query to get a particular element from an array −
> db.getParticularElement.aggregate([ ... { ... $project: ... { ... ElementFromAnArray: 1, ... FourthElement: { $arrayElemAt: [ "$InstructorTechnicalSubject", 3] }, ... ... } ... } ... ]);
The following is the output −
{ "_id" : ObjectId("5c7ee027559dd2396bcfbfb1"), "FourthElement" : "MongoDB" }
- Related Articles
- Extract a particular element from a nested array in MongoDB
- How to get array from a MongoDB collection?
- Extract particular element in MongoDB within a Nested Array?
- How to remove a specific element from array in MongoDB?
- MongoDB query to get record beginning with specific element from an array?
- Projection of arrays to get the first array element from MongoDB documents
- How to delete element from an array in MongoDB?
- Get a single element from the array of results by index in MongoDB
- MongoDB query to pull array element from a collection?
- How do I remove a particular element from an array in JavaScript
- Aggregate a $slice to get an element in exact position from a nested array in MongoDB?
- How to get items from an object array in MongoDB?
- How to list all collections from a particular MongoDB database?
- Removing an array element from a MongoDB collection
- Remove null element from MongoDB array?

Advertisements