This document does not exist, it will not appear in queries or snapshots? Cloud Firestore
Asked Answered
A

3

5

I'm quite new to Cloud Firestore (aren't we all?) and I've added some data to my db using the admin SDK in Node.js. It shows up on the console, but under the doc it says "This document does not exist, it will not appear in queries or snapshots." I'm not sure why this is? Here's a screenshot:enter image description here

Aldo answered 19/10, 2017 at 0:33 Comment(0)
A
4

The key thing to realize is that just because you create a document at root_collection > root_doc > sub_collection > sub_doc does not mean there is actually a document at root_collection > root_doc.

So in order to show you the documents under ... > Events > 10-12-2017 > Phase Data, the console is showing 10-12-2017 as if it was a document, but it's letting you know that there is actually no document at that location. So if you do a query for the documents under ... > Events, 10-12-2017 will not show up.

Aeneous answered 19/10, 2017 at 16:18 Comment(8)
Thanks for the answer! I've decided to just cross back over to the realtime database (yeah i gave up), but ill mark you as correct!Aldo
Sorry to hear that! If you feel like sharing feedback on what caused you to give up or how our docs / examples / console could be more helpful, feel free to drop me a line at [email protected]. Thanks!Aeneous
Hi Michael. I asked a similar question here #47044151. If you have some time, could you take a look? We have several collections that are structured identically but warning is only being shown for some.Felike
@MichaelLehenbauer is there any way to avoid creating a virtual doc and actually create root_doc dynamically when running the code that creates root_collection > root_doc > sub_collection > sub_docMerchant
@MichaelLehenbauer I would love to know the answer to Jaywaa's question tooAnnelleannemarie
I don't think the console lets you create 2 documents at once with a single UI action... but it's possible via the SDKs to do a batch write of both the "parent" doc and the "child" doc to create them simultaneously. They will still be independent and could be deleted separately, etc. though.Aeneous
Further, @MichaelLehenbauer , the batched writes still consider each of the internal operations individually against your firebase billing!!!Runabout
Can anyone answer @Merchant 's question? Is there a way to create non-virtual docs in between, in a way similar to Unix's mkdirs()?Runabout
C
1

I think this is the answer you all are looking for: When you create a document like:

let ref = Firestore.firestore().collection("users").document().collection("data")

you aren't creating an actual location for that document with the auto-generated id. Rather, add an empty field as so:

let tempRef = Firestore.firestore().collection("users").addDocument(data: ["dummy":"text"])
let id = ref.documentId
let ref = Firestore.firestore.collection("users").document(id).collection("data")

I tried it out and works fine now.

Clothilde answered 27/4, 2019 at 12:8 Comment(0)
M
0

Based on suggestion by @rithvik-ravikumar by using Firestore batch (Swift version):

First we creating new document under /users/:id/issues and updating attribute updatedAt on /users/:id to make it "queryable".

func saveInBatch() {

   let db = Firestore.firestore()
   
   var issueReportData: [String: Any] = [:] // Some data you want to store.
   
   let batch = db.batch()
   let userDocRef = db.collection("users").document("_firebase_user_id_goes_here_")
   let issueDocRef = userDocRef.collection("issues").document() // This will be a new document.

   // This is needed in order to make document "queryable".
   batch.setData(["updatedAt": FieldValue.serverTimestamp()], forDocument: userDocRef)
   batch.setData(issueReportData, forDocument: issueDocRef)

   batch.commit() { err in
       if let err = err {
           print("Error writing batch \(err)")
       } else {
           print("Batch write succeeded.")
       }
   }
}

Now we can fetch documents at /users path.

func fetchUsers() {
   let db = Firestore.firestore()
   db.collection("users").getDocuments() { (querySnapshot, err) in
      if let err = err {
         print("Error getting documents: \(err)")
      } else {
         debugPrint("Found \(querySnapshot!.documents.count) documents")
         for document in querySnapshot!.documents {
            let data = document.data()
            print("\(document.documentID) => \(data)")
         }
      }
   }
}
Moise answered 18/7, 2020 at 17:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.