
- 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 do I add a value to the top of an array in MongoDB?
To add a value to the top of an array in MongoDB, you can use unshift() −
yourArrayName.unshift("yourValue");
The above syntax will add the value to the top of an array in MongoDB. Let us first create an array of strings −
> technicalSkills=["Java","MySQL","C","SQL SERVER","ORACLE","PL/SQL"];
This will produce the following output −
[ "Java", "MySQL", "C", "SQL SERVER", "ORACLE", "PL/SQL" ]
Following is the query to add to the top of an array in MongoDB. Here, we will add “MongoDB” to top of an array using unshift() −
> technicalSkills.unshift("MongoDB");
This will produce the following output −
7
Let us check the “MongoDB” has been added to top of an array or not. In order to display the array value, just write the array name at the console −
> technicalSkills
This will produce the following output −
[ "MongoDB", "Java", "MySQL", "C", "SQL SERVER", "ORACLE", "PL/SQL" ]
- Related Articles
- How do I delete array value from a document in MongoDB?
- How do I add an element to an array list in Java?
- How do I push elements to an existing array in MongoDB?
- How do I add a field to existing record in MongoDB?
- How do I get a value array (instead a json array) greater than 50 in MongoDB?
- How do I remove a string from an array in a MongoDB document?
- Script to add a single value to array in MongoDB collection?
- How to add together a subset of elements of an array in MongoDB aggregation?
- How do I order a list and add position to its items in MongoDB?
- Add a field to an embedded document in an array in MongoDB?
- How to maintain the top count of array elements in MongoDB?
- How do I check whether a field contains null value in MongoDB?
- How do I work with array fields in MongoDB to match all?
- How to add new value to an existing array in JavaScript?
- MongoDB query to replace value in an array?

Advertisements