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:
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.
root_doc
dynamically when running the code that creates root_collection > root_doc > sub_collection > sub_doc
–
Merchant mkdirs
()? –
Runabout 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.
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)")
}
}
}
}
© 2022 - 2024 — McMap. All rights reserved.