Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

1 Answer
Samual Sam

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" ]

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.