-->

How to 'skip' and 'include' subdoc

2019-08-17 04:50发布

问题:

Let's say I have this structure

    Account { 
       Id, 
       Name, 
       Password, 
       Details { 
                  LastSignedIn, 
                  CreatedDate
                  Address { 
                            City, 
                            Country
                          } 
               }
        }

Details subdocument belongs to Account, Address subdocument belongs to Details

With mongoose can I say to retrieve the particular subdocument or skip the other one with findqueries?

Something like:

//will return in result 'Account' with only 'DetailsSubdocument' included
Account.findOne({name:'user'}, include: {'Account.DetailsSubdocument'}) 

//will return in result 'Account' with only 'AddressSubdocument' included
Account.findOne({name:'user'}, include: {'Details.AddressSubdocument'})

//will return in result 'Account' both two subdocuments included
Account.findOne({name:'user'}, include: {'Details.AddressSubdocument', 'Account.DetailsSubdocument'})