Use case: find conversations where I am a participant.
Given a collection of conversations is there a way to use a where exists
clause on a nested object's properties? I am using an object with an id as it's key hoping that it would be something I can run a where query against. Should I have put the participants field in an array instead?
db.collection('conversations')
.where('participants.${userId}', 'exists', true)
.get()
.then(() => {
// ...
});
Sample conversation object
{
id: 'foo'
participants: {
userIdA: {
username: 'bar'
},
userIdB: {
username: 'joe'
}
}
}
Assuming that your sample conversation object is a complete example, all you would have to do is set each participating userId equal to true
, and then have your query like this:
db.collection('conversations')
.where(`participants.${userId}`, ==, true)
.get()
.then(snapshot => {
// ...
});
If you needed to know more about a specific user, such as their name, you could just look it up in a users
collection, since you know what their ID is.
db.doc(`users/${userId}`)
.get()
.then(snapshot => {
// ...
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.