This document does not exist and will not appear in queries or snapshots, but identically structured document works

11,386

Solution 1

This is telling you that territories/6 does not exist as an actual document, whereas territories/7 does.

In Cloud Firestore it is possible to have subcollections owned by "virtual" documents - that is the document at the higher level doesn't exist, but it has children.

These virtual documents can be easy ways to organize information without have to create duplicate dummy documents.

In this case, you've either:

  1. Created a bunch of dispatch documents under territories/6 before you created the territories document, or
  2. You've subsequently deleted territories/6 without deleting the subcollection documents.

Solution 2

Not sure this will resolve your problem but did mine... the message in console: "This document does not exist, it will not appear in queries or snapshots" seems to appear if the document has only subcollections but no fields (I guess this is referred to as "virtual" document).

I used set method to add dummy fields (json objects) on already existing "virtual" documents which made them discoverable by snapshots or queries. It did not overwrite/delete any subcollections of those documents. Fields could not be added via console.

My code:

var docRef = this.fsdb.collection('sessions')
//do for all "virtual" docs in collection
docRef.doc('mySession1').set({dummy: 'dummy'})

docRef.onSnapshot(val => {
   var mySessionsArray: any [] = []
   val.forEach(myDoc => {
    mySessionsArray.push(myDoc.id)
  })
  console.log(mySessionsArray)
})

console: ["mySession1", "mySession2", "mySession3"]

Disclaimer, not a programmer by trade :-)

Solution 3

I was able to fix this bug, the accepted answer currently only explains the why and does not provide a fix. Here is the code that caused the bug (for me) when I attempted to run this without a "users" collection at the root of my Firestore:

db.collection("users").document(currentUserUID).collection("groups").addDocument(data: [
        "groupID": newGroup.documentID,
        "userAddDate": NSDate()
    ])

The currentUserUID document that was created was throwing the 'Document does not exist' virtual bug. I copied the currentUserUID and deleted the users collection and virtual UserUID document.

I then created a new 'users' collection at the root node and pasted in the value I had copied for the userUID with a bogus field.Recreate user root node

Then when I re-ran the above snippet of code, the "groups" collection and document were re-added no problem :)

Solution 4

A simple fix

A document needs to have at least one field for it to exist. If a document only contains sub-collections, then it is said to be a virtual/non-existent document and for this reason it is not possible for it to appear in queries or data snapshots.

consider adding a field like this

  // initialize firestoreDatabase

    val firestoreDatabase : FirebaseFirestore? by lazy { FirebaseFirestore.getInstance() }

    // set at least one field
    
    firestoreDatabase.collection("territories").
                                  document("6").
                                  set(hashMapOf(
                              "key" to "value"))

do a similar thing to any other document in your database.

Share:
11,386
wizloc
Author by

wizloc

Updated on June 26, 2022

Comments

  • wizloc
    wizloc about 2 years

    Preface: this question is already asked here but user gave up and gave solution to first answer. This question also differs in that I have two similar collection structures, but the error only occurs on one of them.

    I am working with Google's new firestore database, and have created the following structure: territories/{list of territories}/dispatches/{list of dispatches}/{dispatch information}

    We are using this method to create custom tokens on our backend using Firebase Admin SDK. When a user logs in on our backend, we generate the token and add the territories they have access to as additional claims, which we intend to access from the auth / request.auth objects in our Security Rules to limit their access to the dispatch documents accordingly. I mention this in case we are going about the structure incorrectly, in which case please correct me as we are new to firestore.

    The problem we are encountering is that one of the documents gives the warning: "This document does not exist and will not appear in queries or snapshots" (see image below). However, we have an identical document structure (document 7 in the image) that does not give this warning and does appear in queries and snapshots. enter image description here

  • Jaywaa
    Jaywaa over 6 years
    Is there a way to dynamically create documents when creating sub-collections then? i.e if OP wanted to add territories/10/dispatches/... would he first be required to add 10, then dispatches? One could infer that: db.collections("territories").document("10").collection("dis‌​patches").add("...") would do this for him, but I see this is not the case.
  • varun
    varun about 5 years
    Can you please answer @Jaywaa 's question? Is there a way to create non-virtual documents while creating long chains of collection and docs? Please let us know!
  • Dan McGrath
    Dan McGrath about 5 years
    Separate questions should be asked as new Stack Overflow questions rather than in the comments section. This makes it easier to answer, makes sure the question/answer stays on a single topic, and also means more people are likely to see it and answer :)