How to enter data in mongodb array at specific position such that if there is only 2 data in array and I want to insert at 5, then rest data is null

33 views Asked by At

This is the original data that I want to edit -

"Languages" : [
        "English",
        "French"
    ],

And this is what I want to achieve by inserting the data such that it will leave 2 null values if there is empty spaces when entering the data at position 5 and if there is only 1 empty space then it should only add 1 null data in the array and then it should add Tamil into the array -

"Languages" : [
        "English",
        "French",
        null,
        null,
        "Tamil"
    ],

This is what I tried -

$push: {
       "Languages" : {
            $each: ["Tamil"],
            $position: 4
        }
}
0

There are 0 answers