- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Insert to specific index for MongoDB array?
To insert a specific index for MongoDB array, you can use $push operator. Let us create a collection with documents
>db.insertToSpecificIndexDemo.insertOne({"StudentName":"Larry","StudentSubjects":["MySQL","Java"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c9d2562a629b87623db1b2c") } >db.insertToSpecificIndexDemo.insertOne({"StudentName":"Chris","StudentSubjects":["C++","C"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c9d2573a629b87623db1b2d") }
Following is the query to display all documents from a collection with the help of find() method
> db.insertToSpecificIndexDemo.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5c9d2562a629b87623db1b2c"), "StudentName" : "Larry", "StudentSubjects" : [ "MySQL", "Java" ] } { "_id" : ObjectId("5c9d2573a629b87623db1b2d"), "StudentName" : "Chris", "StudentSubjects" : [ "C++", "C" ] }
Following is the query to insert to a specific index for MongoDB array in _id “5c9d2573a629b87623db1b2d”
> db.insertToSpecificIndexDemo.update( ... { _id: ObjectId("5c9d2573a629b87623db1b2d")}, ... { $push: { ... StudentSubjects: { ... $each: [ {"CoreSubject": "MongoDB"} ], ... $position: 0 ... } ... }} ... ); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Let us check the value is inserted into the specific position or not. Above, we have given index 0 that would mean insertion in the beginning
> db.insertToSpecificIndexDemo.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5c9d2562a629b87623db1b2c"), "StudentName" : "Larry", "StudentSubjects" : [ "MySQL", "Java" ] } { "_id" : ObjectId("5c9d2573a629b87623db1b2d"), "StudentName" : "Chris", "StudentSubjects" : [ { "CoreSubject" : "MongoDB" }, "C++", "C" ] }
Look at the sample output, the “CoreSubject”:”MongoDB” is inserted at the beginning in the MongoDB array.
- Related Articles
- Update object at specific Array Index in MongoDB?
- MongoDB bulk insert for documents
- Python Pandas - Insert a new index value at a specific position
- Insert data into inner array in MongoDB?
- How do I insert elements at a specific index in Java list?
- Query Array for 'true' value at index n in MongoDB?
- Querying on an array of objects for specific nested documents with MongoDB?
- MongoDB query to update array object in index N?
- MongoDB query to insert an array element with a condition?
- MongoDB query to return specific fields from an array?
- Writing a MongoDB insert statement for multiple insert at a time
- MongoDB query for documents whose array elements does not have a specific value
- How to improve MongoDB queries with multikey index in array?
- How to get a specific object from array of objects inside specific MongoDB document?
- How to remove a specific element from array in MongoDB?

Advertisements