How to query Mongodb multi-layer nested arrays accurately?

  mongodb, question
{
 "excelID": "10001",
 "excelName": "First Year Student Information",
 "sheetArrays": [
 {
 "sheetName": "squad one",
 "studentInfos": [
 {
 "name": "Zhang San",
 "sex": "male"
 },
 {
 "name": "Li Si",
 "sex": "male"
 }
 ]
 },
 {
 "sheetName": "class 2",
 "studentInfos": [
 {
 "name": "Wang Wu",
 "sex": "male"
 },
 {
 "name": "Zhao Liu",
 "sex": "female"
 }
 ]
 }
 ]
 }

I hope to find out the names of all sex men through one sentence.

I also hope that I can find out the class name corresponds to Wang Wu and his basic information through a sentence, and ask my friends to answer my questions. I am very grateful.

What is the concept of a sentence?aggregationIs it a statement?

db.test3.aggregate([
 {$ match: {"sheetarrays.studentinfos.sex":' male'}}},
 {$unwind: "$sheetArrays"},
 {$unwind: "$sheetArrays.studentInfos"},
 {$ match: {"sheetarrays.studentinfos.sex":' male'}}},
 ]);
 db.test3.aggregate([
 {$ match: {"sheetarrays.studentinfos.name":' Wang Wu'}}},
 {$unwind: "$sheetArrays"},
 {$unwind: "$sheetArrays.studentInfos"},
 {$ match: {"sheetarrays.studentinfos.name":' Wang Wu'}}},
 ]);

Although it can be done, in fact such a data structure is not reasonable.
MongoDB’s model design is a big topic. Those who are interested can read the documents carefully.data modelingChapters.
The basic principle is actually that there is no principle, which is much more troublesome than satisfying the relationship paradigm. There is no rule telling you what to do. But basically, the data model you choose should take into account how to use various scenes, how to update, how to insert, how to query … A model that can well satisfy the use of scenes is a good model.
Go back to your model and see what’s wrong:

  1. Take WT engine as an example, document level lock, when anyone in any class changes, changes will be made to this document, that is to say, these operations are all mutually exclusive, and the efficiency can be imagined as poor.

  2. As you can see from the above query, it is neither elegant nor efficient. Not to mention when updating, think about all the trouble.
    So this is not a good model. MongoDB advocates anti-normal form, but don’t be so extreme and anti-normal form for anti-normal form. Try a more convenient model for yourself.