Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 713
wizloc Avatar asked Oct 31 '17 20:10

wizloc


4 Answers

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 :-)

like image 149
Juraj Avatar answered Nov 06 '22 03:11

Juraj


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.

like image 42
Eric Gacoki Avatar answered Nov 06 '22 02:11

Eric Gacoki


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.
like image 14
Dan McGrath Avatar answered Nov 06 '22 02:11

Dan McGrath


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 :)

like image 2
N0ug4t Avatar answered Nov 06 '22 03:11

N0ug4t