I have a structure like this:
user/user_id/books/book_id
I'd like to get the collection of books of a user that have a specific phone. So I'd use:
firestore.collection('user').where('phone', '==', specificPhone)
But after that, how do I get the books subcollection? Do I have to manually do another query?
I only want to get the first coincidence, since where
returns a set of results. Also I want to retrieve the data only once, not listen to changes.
Firestore reads are shallow; they don't read data from subcollections. So you'll indeed need to perform another read operation to get the books for the users that you found in your first query.
For example:
let user = await firestore.collection('user').where('phone', '==', specificPhone).get();
let books = await user.ref.collection("books").get();
books.forEach((bookDoc) => {
console.log(book.id, book.data());
});
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments