How do I create a new collection?











up vote
1
down vote

favorite












I'm building a chat app where there are GroupConversations and GroupMessages. Following the example of some of the Firestore YouTube videos, I've chosen to structure my data this way:



The ID of the GroupConversation is the ID for the collection of GroupMessages associated with that GroupConversation. Then inside of that document should be a new collection called Messages which holds documents.



Does that make sense or am I overcomplicating things? I can't seem to create that Messages collection or set anything to it in my Swift code



GroupConversations
id
title
desc
memberIds

GroupMessages
GroupConversationID
createdAt: Date // just because I need a key and a document?
Messages (new collection)
Message1
Message2
Message3


Thanks



Edit: Added Swift code



        let db = Firestore.firestore()
var data: [String: Any] = [
"text": title,
"senderUsername": username,
"createdAt": Date(),
"updatedAt": Date()
]

let creationData: [String: Any] = [
"text": "Group Created",
"createdAt": Date()
]

let doc = db.collection("GroupMessages").addDocument(data: creationData)
db.document("GroupMessages/(groupConvo.documentId)/(doc.documentID)/messages").setData(data) { error in
if error == nil {
completion()
}
}


In my console, I'm seeing a new GroupMessage object created but not sub-collection of that GroupMessage called messages










share|improve this question




























    up vote
    1
    down vote

    favorite












    I'm building a chat app where there are GroupConversations and GroupMessages. Following the example of some of the Firestore YouTube videos, I've chosen to structure my data this way:



    The ID of the GroupConversation is the ID for the collection of GroupMessages associated with that GroupConversation. Then inside of that document should be a new collection called Messages which holds documents.



    Does that make sense or am I overcomplicating things? I can't seem to create that Messages collection or set anything to it in my Swift code



    GroupConversations
    id
    title
    desc
    memberIds

    GroupMessages
    GroupConversationID
    createdAt: Date // just because I need a key and a document?
    Messages (new collection)
    Message1
    Message2
    Message3


    Thanks



    Edit: Added Swift code



            let db = Firestore.firestore()
    var data: [String: Any] = [
    "text": title,
    "senderUsername": username,
    "createdAt": Date(),
    "updatedAt": Date()
    ]

    let creationData: [String: Any] = [
    "text": "Group Created",
    "createdAt": Date()
    ]

    let doc = db.collection("GroupMessages").addDocument(data: creationData)
    db.document("GroupMessages/(groupConvo.documentId)/(doc.documentID)/messages").setData(data) { error in
    if error == nil {
    completion()
    }
    }


    In my console, I'm seeing a new GroupMessage object created but not sub-collection of that GroupMessage called messages










    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I'm building a chat app where there are GroupConversations and GroupMessages. Following the example of some of the Firestore YouTube videos, I've chosen to structure my data this way:



      The ID of the GroupConversation is the ID for the collection of GroupMessages associated with that GroupConversation. Then inside of that document should be a new collection called Messages which holds documents.



      Does that make sense or am I overcomplicating things? I can't seem to create that Messages collection or set anything to it in my Swift code



      GroupConversations
      id
      title
      desc
      memberIds

      GroupMessages
      GroupConversationID
      createdAt: Date // just because I need a key and a document?
      Messages (new collection)
      Message1
      Message2
      Message3


      Thanks



      Edit: Added Swift code



              let db = Firestore.firestore()
      var data: [String: Any] = [
      "text": title,
      "senderUsername": username,
      "createdAt": Date(),
      "updatedAt": Date()
      ]

      let creationData: [String: Any] = [
      "text": "Group Created",
      "createdAt": Date()
      ]

      let doc = db.collection("GroupMessages").addDocument(data: creationData)
      db.document("GroupMessages/(groupConvo.documentId)/(doc.documentID)/messages").setData(data) { error in
      if error == nil {
      completion()
      }
      }


      In my console, I'm seeing a new GroupMessage object created but not sub-collection of that GroupMessage called messages










      share|improve this question















      I'm building a chat app where there are GroupConversations and GroupMessages. Following the example of some of the Firestore YouTube videos, I've chosen to structure my data this way:



      The ID of the GroupConversation is the ID for the collection of GroupMessages associated with that GroupConversation. Then inside of that document should be a new collection called Messages which holds documents.



      Does that make sense or am I overcomplicating things? I can't seem to create that Messages collection or set anything to it in my Swift code



      GroupConversations
      id
      title
      desc
      memberIds

      GroupMessages
      GroupConversationID
      createdAt: Date // just because I need a key and a document?
      Messages (new collection)
      Message1
      Message2
      Message3


      Thanks



      Edit: Added Swift code



              let db = Firestore.firestore()
      var data: [String: Any] = [
      "text": title,
      "senderUsername": username,
      "createdAt": Date(),
      "updatedAt": Date()
      ]

      let creationData: [String: Any] = [
      "text": "Group Created",
      "createdAt": Date()
      ]

      let doc = db.collection("GroupMessages").addDocument(data: creationData)
      db.document("GroupMessages/(groupConvo.documentId)/(doc.documentID)/messages").setData(data) { error in
      if error == nil {
      completion()
      }
      }


      In my console, I'm seeing a new GroupMessage object created but not sub-collection of that GroupMessage called messages







      ios swift google-cloud-firestore






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 at 23:57

























      asked Nov 19 at 23:35









      Zack Shapiro

      1,09673981




      1,09673981
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          You're not building the path to the document correctly. Document paths always alternate between collection and document names. To build a path to a document in a subcollection, it would go collection/document/subcollection/document. It looks like you've mixed up the last two elements in the string you're building.



          In my opinion, it's harder to go wrong if you build up the document using methods rather than trying to build a string. Something like this:



          db
          .collection("GroupMessages")
          .document(groupConvo.documentId)
          .collection("Messages")
          .document(doc.documentId)
          .setData(...)





          share|improve this answer





















          • Ahhh, had those backwards. Yeah that build method is a lot better than my string gibberish. Thanks again Doug!
            – Zack Shapiro
            Nov 20 at 0:12











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384187%2fhow-do-i-create-a-new-collection%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          You're not building the path to the document correctly. Document paths always alternate between collection and document names. To build a path to a document in a subcollection, it would go collection/document/subcollection/document. It looks like you've mixed up the last two elements in the string you're building.



          In my opinion, it's harder to go wrong if you build up the document using methods rather than trying to build a string. Something like this:



          db
          .collection("GroupMessages")
          .document(groupConvo.documentId)
          .collection("Messages")
          .document(doc.documentId)
          .setData(...)





          share|improve this answer





















          • Ahhh, had those backwards. Yeah that build method is a lot better than my string gibberish. Thanks again Doug!
            – Zack Shapiro
            Nov 20 at 0:12















          up vote
          1
          down vote



          accepted










          You're not building the path to the document correctly. Document paths always alternate between collection and document names. To build a path to a document in a subcollection, it would go collection/document/subcollection/document. It looks like you've mixed up the last two elements in the string you're building.



          In my opinion, it's harder to go wrong if you build up the document using methods rather than trying to build a string. Something like this:



          db
          .collection("GroupMessages")
          .document(groupConvo.documentId)
          .collection("Messages")
          .document(doc.documentId)
          .setData(...)





          share|improve this answer





















          • Ahhh, had those backwards. Yeah that build method is a lot better than my string gibberish. Thanks again Doug!
            – Zack Shapiro
            Nov 20 at 0:12













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          You're not building the path to the document correctly. Document paths always alternate between collection and document names. To build a path to a document in a subcollection, it would go collection/document/subcollection/document. It looks like you've mixed up the last two elements in the string you're building.



          In my opinion, it's harder to go wrong if you build up the document using methods rather than trying to build a string. Something like this:



          db
          .collection("GroupMessages")
          .document(groupConvo.documentId)
          .collection("Messages")
          .document(doc.documentId)
          .setData(...)





          share|improve this answer












          You're not building the path to the document correctly. Document paths always alternate between collection and document names. To build a path to a document in a subcollection, it would go collection/document/subcollection/document. It looks like you've mixed up the last two elements in the string you're building.



          In my opinion, it's harder to go wrong if you build up the document using methods rather than trying to build a string. Something like this:



          db
          .collection("GroupMessages")
          .document(groupConvo.documentId)
          .collection("Messages")
          .document(doc.documentId)
          .setData(...)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 at 0:05









          Doug Stevenson

          66.6k87997




          66.6k87997












          • Ahhh, had those backwards. Yeah that build method is a lot better than my string gibberish. Thanks again Doug!
            – Zack Shapiro
            Nov 20 at 0:12


















          • Ahhh, had those backwards. Yeah that build method is a lot better than my string gibberish. Thanks again Doug!
            – Zack Shapiro
            Nov 20 at 0:12
















          Ahhh, had those backwards. Yeah that build method is a lot better than my string gibberish. Thanks again Doug!
          – Zack Shapiro
          Nov 20 at 0:12




          Ahhh, had those backwards. Yeah that build method is a lot better than my string gibberish. Thanks again Doug!
          – Zack Shapiro
          Nov 20 at 0:12


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384187%2fhow-do-i-create-a-new-collection%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'