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

8

14

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

Bendicta answered 31/10, 2017 at 20:1 Comment(0)
A
14

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.
Amitosis answered 1/11, 2017 at 2:36 Comment(3)
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("dispatches").add("...") would do this for him, but I see this is not the case.Burdened
Can you please answer @Burdened 's question? Is there a way to create non-virtual documents while creating long chains of collection and docs? Please let us know!Ishmael
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 :)Amitosis
C
14

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"]

Commeasure answered 20/11, 2017 at 5:44 Comment(0)
A
2

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

Adversity answered 3/5, 2018 at 20:48 Comment(0)
S
2

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.

Subbasement answered 3/9, 2020 at 23:28 Comment(0)
H
1

Simple solution to overcome this problem, while keeping thigs tide.

Cause:

  • It figures that the document will not be created if there is no field in it
  • Nested collection is not a field and therefore the document is concidered to be empty and despite the fact appears in the console:
  1. shown as greyed-out in the console.
  2. It does not really exist (not returned from queries/snapshots).

I started by adding a "Dummy" field to the document.

_firestore
      .collection(collectionName)
      .doc(dbName)
      .set({"dummy": "dummy"});

Then I added all the collections under the document (in my case 4 collections).

_firestore
      .collection(collectionName)
      .doc(dbName)
      .collection(somePackageName)
      .add(a MAP of the data);

And finally, I have deleted the dummy field from the document.

 _firestore
       .collection(collectionName)
       .doc(dbName)
       .update({"dummy": FieldValue.delete()});
Harijan answered 4/7, 2022 at 7:58 Comment(0)
S
0

Here is how I understand it. In Firebase Cloud Firestore, you can create a document without creating its parent document. In other words, it's perfectly OK to add "mysubdoc" without creating "mydoc" beforehand.

[Kotlin]
FirebaseFirestore.getInstance()
    .document("/mycoll/mydoc/mysubcoll/mysubdoc").set(hashMapOf("key", "value"))

If you do, there is no harm, except that you get "This document does not exist, it will not appear in queries or snapshots" warning and possibly some queries might not work as expected.

Conversely, deleting a document does not delete its subcollections. Refer to https://firebase.google.com/docs/firestore/manage-data/delete-data for more info.

If you do want to create the parent document, you can do something similar to the following; create a document with an empty value.

FirebaseFirestore.getInstance()
    .document("/mycoll/mydoc").set(hashMapOf<String, String>())

Make sure the read/write rules are correctly set, especially if you use the user's ID as a part of the document path to prevent unauthorized users from snooping around.

If it's just a one-time deal, you can create "mydoc" from Firebase console as well.

Saltwort answered 8/1, 2020 at 0:56 Comment(0)
A
0

This usually occurs when there is an empty field in the collection, and can be solved by adding a dummy value to the field, preferably the current time stamp should suffice.

Aboulia answered 11/8, 2021 at 7:2 Comment(0)
G
0

documents need fields to exist. You just have to add a field in the doc and it's good

Gherardo answered 29/4, 2023 at 13:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.