
- 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
MongoDB query to get record beginning with specific element from an array?
You can use dot(.) notation along with array index to get record beginning with specific element. Let us first create a collection with documents −
>db.arrayStartsWithElementDemo.insertOne({"PlayerName":"Chris","PlayerScore":[780,9000,456,789,987]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd29fed345990cee87fd889") } >db.arrayStartsWithElementDemo.insertOne({"PlayerName":"Robert","PlayerScore":[890,670,890,54646,42424]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd2a00c345990cee87fd88a") } >db.arrayStartsWithElementDemo.insertOne({"PlayerName":"David","PlayerScore":[909090,896555,3344433,78900]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd2a025345990cee87fd88b") }
Following is the query to display all documents from a collection with the help of find() method −
> db.arrayStartsWithElementDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd29fed345990cee87fd889"), "PlayerName" : "Chris", "PlayerScore" : [ 780, 9000, 456, 789, 987 ] } { "_id" : ObjectId("5cd2a00c345990cee87fd88a"), "PlayerName" : "Robert", "PlayerScore" : [ 890, 670, 890, 54646, 42424 ] } { "_id" : ObjectId("5cd2a025345990cee87fd88b"), "PlayerName" : "David", "PlayerScore" : [ 909090, 896555, 3344433, 78900 ] }
Following is the query to get array beginning with specific elements 890 and then 670 −
> db.arrayStartsWithElementDemo.find({"PlayerScore.0" : 890, "PlayerScore.1" : 670}).pretty();
This will produce the following output. Here,
{ "_id" : ObjectId("5cd2a00c345990cee87fd88a"), "PlayerName" : "Robert", "PlayerScore" : [ 890, 670, 890, 54646, 42424 ] }
- Related Articles
- MongoDB query to return specific fields from an array?
- Delete specific record from an array nested within another array in MongoDB?
- Deleting specific record from an array nested within another array in MongoDB
- MongoDB query to find a specific city record from a collection
- MongoDB query to match and remove element from an array?
- MongoDB query to insert an array element with a condition?
- How to get a specific column record from SELECT query in MySQL?
- MongoDB query to get only specific fields in nested array documents?
- MongoDB query to get average in aggregation of array element?
- Query an array in MongoDB to fetch a specific value
- MongoDB query to pull array element from a collection?
- MongoDB query to get specific list of names from documents where the value of a field is an array
- Get Random record from MongoDB?
- Query MongoDB with “like” implementation on name and email field beginning with a specific letter?
- How to remove a specific element from array in MongoDB?

Advertisements