0

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'})
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Sergino
  • 10,128
  • 30
  • 98
  • 159
  • How is DetailsSubdocument linked to `Account`? – Explosion Pills Feb 13 '16 at 02:09
  • @ExplosionPills just updated – Sergino Feb 13 '16 at 02:17
  • Not 100% sure what you are going for since `Details` contains `Address` but it seems like you can do this with projection (second argument to `.findOne` as in `.findOne({name:'user', {"Details.LastSignedIn": 1, "Details.CreatedDate": 1})` for the first, and then `{"Details.Address": 1}`, and just `{"Details": 1}` to include everything. – Explosion Pills Feb 13 '16 at 02:20

0 Answers0