Reputation: 57
I have a database with the following structure in Firestore:
users/{user document}/requests/{request document}
EDIT: I added a screenshot of my firebase console showing the data. A little hard to see but you can tell that the record for the user as well as the request exist. You should also be able to see the 'path'. I hope that helps.
I need to query the requests collection for a particular user (so this is not a CollectionGroup thing). I'm using Angular Firebase and try the following
var ref = db.collection("users").doc("0g0ujmsgdTa7NgaJPUV5ULjumIV2").collection("requests")
var result = await ref.where("to","==","frank").get();
console.log(result.size);
This returns a result of zero records (where the presence of the user document has been confirmed (that long string) and that multiple documents in the requests collection contain the field 'to' which is set to 'frank'.
By all accounts this should work. I've tried many other queries like pulling down all the records of the requests collection (meaning a get() on the requests collection), and it works. I've tried similar queries on the user collection (with data relevant to users) and that works.
Is there anything I can try to trouble shoot why this would happen?
Upvotes: 1
Views: 1626
Reputation: 317352
The data you're trying to query is nested as a field under a map field called "inRequest". You need to identify the full path of this field for querying using dot notation:
var ref = db
.collection("users")
.doc("0g0ujmsgdTa7NgaJPUV5ULjumIV2")
.collection("requests")
var result = await ref
.where("inRequest.to", "==", "frank")
.get();
console.log(result.size);
Note the first argument to where()
.
Upvotes: 2
Reputation: 4808
based on documentation and if my understanding is correct you should do
var myCollection = db.collection("users").doc("0g0ujmsgdTa7NgaJPUV5ULjumIV2").collection("requests", ref => ref.where('to', '==', 'frank'))
myCollection.get().subscribe((response) => console.log(response))
where
is not a method of firebase.firestore.collection
rather one of firebase.firestore.CollectionReference.
also you won't get anything more than an Observable
using get()
method
(not tested)
REFERENCE
Upvotes: 1