是否有"存在"子句可以与火堆一起使用?



用例:查找我是参与者的对话。

给定一组对话,有没有办法在嵌套对象的属性上使用 whereexists子句? 我正在使用一个带有 id 的对象作为关键,希望它可以是我可以对其运行 where 查询的东西。 我应该将参与者字段放在数组中吗?

db.collection('conversations')
.where('participants.${userId}', 'exists', true)
.get()
.then(() => {
// ...
});

示例对话对象

{ 
id: 'foo'
participants: { 
userIdA: {
username: 'bar'
},
userIdB: {
username: 'joe'
}
}
}

假设您的示例对话对象是一个完整的示例,您所要做的就是将每个参与的 userId 设置为等于true,然后像这样进行查询:

db.collection('conversations')
.where(`participants.${userId}`, ==, true)
.get()
.then(snapshot => {
// ...
});

如果您需要了解有关特定用户的更多信息,例如他们的名字,您可以在users集合中查找它,因为您知道他们的 ID 是什么。

db.doc(`users/${userId}`)
.get()
.then(snapshot => {
// ...
});

最新更新