Mongoose conditional query?

problem description

if you want to get a piece of data required by composition in the course field, the data returned after using find contains excess data, and the result is not ideal. In order to achieve this goal, how to write code, it seems that beginners in moongse, need to use populate or aggregate . How should this be written? Thank you!

related codes

schema

const courseInf = new Schema({
    teacher:String,
    courseName:String,
});
const student = new Schema({
    name:String,                //
    classname:String,                //
    stunum:String,                //
    oldpassword:String,           //
    newpassword:String,           //
    phonenum:String,              //
    course:[courseInf]           //
});

data

{ _id: 5b7a97508e00501ceaf66030,
  name: "",
  class: "1",
  stunum: "111",
  oldpassword: "111",
  course:
   [ { _id: 5b7a97508e00501ceaf66032,
       teacher: "",
       courseName: ""
      },
     { _id: 5b7a97508e00501ceaf66031,
       teacher: "",
       courseName: ""
        } ],
  __v: 0 }

what result do you expect? What is the error message actually seen?

want to return only this single piece of data

 course:
   [ { _id: 5b7a97508e00501ceaf66032,
       teacher: "",
       courseName: ""
      }]

it turns out that my way of writing is student.find ({stunum:"1",courseName:" higher Mathematics", teacher:" Li Si"});
but the returned result is

course:
   [ { _id: 5b7a97508e00501ceaf66032,
       teacher: "",
       courseName: ""
      },
     { _id: 5b7a97508e00501ceaf66031,
       teacher: "",
       courseName: ""
        } ]}

how to get the desired results, please give me some advice, thank you!

Apr.26,2021

student.aggregate([
    {
        $match:{stunum:'1'}
    },{
        $unwind:{
            path:'$course',
            preserveNullAndEmptyArrays: true,
        }
    },{
        $match:{courseName:'',teacher:''}
    },{
        $project:{
            course:1
        }
    }
])

this should get

course:{ 
    _id: 5b7a97508e00501ceaf66032,
    teacher: '',
    courseName: ''
}

if you just want to modify the value, you don't need to take it out, just modify it

student.update(
    {stunum:'1'},
    {
        $set:{'course.$[element].teacher':''}
    },
    {arrayFilters:[{'element.teacher':''}]}
)
Menu