
- 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
Getting only the first item for an array property in MongoDB?
Use $slice operator for this. Let us first create a collection with documents −
> db.gettingFirstItemInArrayDemo.insertOne( { "UserId": 101, "UserName":"Carol", "UserOtherDetails": [ {"UserFriendName":"Sam"}, {"UserFriendName":"Mike"}, {"UserFriendName":"David"}, {"UserFriendName":"Bob"} ] } ); { "acknowledged" : true, "insertedId" : ObjectId("5cdfca52bf3115999ed51205") }
Following is the query to display all documents from a collection with the help of find() method −
> db.gettingFirstItemInArrayDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cdfca52bf3115999ed51205"), "UserId" : 101, "UserName" : "Carol", "UserOtherDetails" : [ { "UserFriendName" : "Sam" }, { "UserFriendName" : "Mike" }, { "UserFriendName" : "David" }, { "UserFriendName" : "Bob" } ] }
Following is the query to get only the first item for an array property in MongoDB −
> db.gettingFirstItemInArrayDemo.find({"UserId":101}, {UserOtherDetails:{$slice: 1}});
This will produce the following output −
{ "_id" : ObjectId("5cdfca52bf3115999ed51205"), "UserId" : 101, "UserName" : "Carol", "UserOtherDetails" : [ { "UserFriendName" : "Sam" } ] }
- Related Articles
- Search for documents matching first item in an array with MongoDB?
- Get the first and last item in an array using JavaScript?
- MongoDB query to set a sub item in an array?
- Removing item from array in MongoDB?
- How to update a MongoDB document for adding a new item to an array?
- Display only an element found in an array in MongoDB?
- How to move specific item in array list to the first item in Java?
- MongoDB query to find property of first element of array?
- Can I get the first item in a Cursor object in MongoDB?
- How to insert an item to an array that is inside an object in MongoDB?
- Retrieve only the queried element in an object array in MongoDB collection?
- How to get only the first n% of an array in JavaScript?
- MongoDB query to remove item from array?
- MongoDB query to gather unique array item?
- Golang Program To Get The First Item From The Array

Advertisements