title: Is there a way to map a sub-collection into a struct while querying the main collection? (Firestore)
body: <p>Is there a way to map a sub-collection into a struct while querying the main collection?</p>
  <p>My sub-collection structure is:</p>
  <pre><code>-conversations (collection)
  --- &quot;users&quot; : [array of two users]
  --- messages (subcollection)
  ------- &quot;created by&quot;: &quot;sender&quot;
  ------- &quot;date&quot; : timestamp
  ------- &quot;msg&quot; : &quot;help me guys I'm stuck with this damn chat feature&quot;
  </code></pre>
  <p>I read somewhere the query are shallow meaning you'll only get the fields of the collecting you're querying, but maybe there is another way besides nested query loops after querying the first collection</p>
  <pre><code>func getFilteredConversations(query: String) {
          
          if (user != nil) {
              db.collection(&quot;conversations&quot;).whereField(&quot;users&quot;, arrayContains: user!.displayName)
                  .order(by: &quot;createdTime&quot;)
                  .addSnapshotListener { (querySnapshot, error) in
                      guard let documents = querySnapshot?.documents else {
                          print(&quot;no conversations found&quot;)
                          return
                      }
                      
                      //mapping
                      self.chats = documents.map {(queryDocumentSnapshot) -&gt; Conversation in
                          
                          let data = queryDocumentSnapshot.data()
                          let docId = queryDocumentSnapshot.documentID
                          let users = data[&quot;users&quot;] as? [String] ?? [&quot;&quot;]
                          let unreadmsg = data[&quot;hasUnreadMessage&quot;] as? Bool ?? false
                          
                          //MARK: - GET MESSAGES
                          self.db.collection(&quot;conversations&quot;).document(docId).collection(&quot;messages&quot;)
                              .order(by: &quot;date&quot;)
                              .addSnapshotListener{ (querySnapshot, err) in
                                  
                                  guard let documents = querySnapshot?.documents else {
                                      print(&quot;no messages found&quot;)
                                      return
                                  }
                                  
                                  var mensajes = [Message]()
                                  
                                  mensajes = documents.map {(queryDocumentSnapshot) -&gt; Message in
                                      
                                      let data = queryDocumentSnapshot.data()
                                      let docId = queryDocumentSnapshot.documentID
                                      let createdby = data[&quot;created_by&quot;] as? String ?? &quot;&quot;
                                      let msg = data[&quot;msg&quot;] as? String ?? &quot;&quot;
                                      let date = data[&quot;date&quot;] as? Timestamp ?? Timestamp()
                                      
                                      return Message(createdBy: createdby, msg: msg, date: date, id: docId)
                                      
                                  }
                              }
                          
                          print(&quot;Users: \(users)&quot;)
                          
                          return Conversation(id: docId, users: users, messages: mensajes, hasUnreadMessage: unreadmsg)
                          
                      }
                  }
          }
      }
  
  </code></pre>
  <p>this is the model</p>
  <pre><code>struct Conversation: Decodable, Identifiable {
      //var id: UUID { person.id }
      @DocumentID var id: String? = UUID().uuidString
      var users: [String] = [&quot;&quot;]
      var messages: [Message] = []
      var hasUnreadMessage : Bool = false
  }
  
  struct Message: Decodable {
      var createdBy: String?
      var msg: String?
      @ServerTimestamp var date : Timestamp?
      var id : String?
  }
  </code></pre>