Add value to new AddFields Array

{
“_id” : 1,
“student” : “Maya”,
“homework” : [ 10, 5, 10 ],
“quiz” : [ 10, 8 ],
“extraCredit” : 0,
“totalHomework” : 25,
“totalQuiz” : 18,
“totalScore” : 43
}
{
“_id” : 2,
“student” : “Ryan”,
“homework” : [ 5, 6, 5 ],
“quiz” : [ 8, 8 ],
“extraCredit” : 8,
“totalHomework” : 16,
“totalQuiz” : 16,
“totalScore” : 40
}

how to add new fields with array
student element value add into one array like this.

studentArray : [Maya, Ryan]

Hi there,

Remember that not getting what you want is sometimes a wonderful stroke of luck.
Dalai Lama

What did you try?

I want try retrieve by Id example _id = 2

{
“_id” : 2,
“student” : “Ryan”,
“homework” : [ 5, 6, 5 ],
“quiz” : [ 8, 8 ],
“extraCredit” : 8,
“totalHomework” : 16,
“totalQuiz” : 16,
“totalScore” : 40,
“studentArray” : [Maya, Ryan], //add new/create field for combine two element value from id 1 and 2
}

Interesting question. I would do it in two steps, for example, maybe something like this:

async function myAsyncCall(){
  const first = await db.collection.find({_id:2})
  const result =  await db.collection.updateOne( 
    {_id:1}, 
    {$push:{studentArray:first.student}}
  )
  console.log(result)
}

myAsyncCall()

I am not sure how easy is to run this in MDB Charts.

Please read Formatting code and log snippets in posts and update your sample documents so that we can cut-n-paste into our system.