I am writing a query in react native to get data from firestore by filtration of friends by friend name ... Like get all useers where friendName is XYZ
[
{User: 'Johan', friends:[{friendName: 'ABC', age:20}, {friendName: 'DEF', age:30},{friendName: 'XYZ', age:25}]},
{User: 'Johnson', friends:[{friendName: 'ABC', age:20}, {friendName: 'XYZ', age:25}]},
{User: 'Symonds', friends:[{friendName: 'DEF', age:30},{friendName: 'XYZ', age:25}]}
]
I am trying with code like this by is not working. As I need to mention to filter with friendName but not able to find how to write this in query.
usersRef
.doc(userId)
.collection("Users")
.where('friends', 'array-contains','ABC')
.get();
as long as I Know you can't query fields inside objects. My approach would be querying user's id, and then iterate resulting array.
//First make a query
let resultsArray=[]
usersRef.doc(userId).collection("Users").get().then((docs)=>{
this.resultsArray.push(docs)
})
//after query is done, iterate results
this.resultsArray.foreach((item)=>{
item.data().friends.foreach((friend)=>{
if(friend.friendName === 'ABC'){
//yes, your friend name is ABC, do whatever you need.
}
)
})