[英]Is there an `exists` where clause I can use with firestore?
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? 给定一系列对话,是否可以在嵌套对象的属性上使用where
exists
子句? 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. 我正在使用具有id的对象,因为它的关键是希望可以对它进行where查询。 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: 假设您的示例对话对象是一个完整的示例,您要做的就是将每个参与的userId设置为
true
,然后进行如下查询:
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. 如果您需要更多有关特定用户的信息,例如他们的姓名,则可以在
users
集合中查找它,因为您知道他们的ID是什么。
db.doc(`users/${userId}`)
.get()
.then(snapshot => {
// ...
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.