Javascript 多个字段中相同值的Firestore查询
是否可以在cloud firestore中查询文档多个字段中相同值的数据Javascript 多个字段中相同值的Firestore查询,javascript,firebase,react-native,google-cloud-firestore,Javascript,Firebase,React Native,Google Cloud Firestore,是否可以在cloud firestore中查询文档多个字段中相同值的数据 这样的示例 await firestore() .collection('chats') .where('ownerUserId', '==', user.uid) .where('chatUserId', '==', user.uid) .orderBy('createdAt', 'desc') .get() await firestore() .
这样的示例
await firestore()
.collection('chats')
.where('ownerUserId', '==', user.uid)
.where('chatUserId', '==', user.uid)
.orderBy('createdAt', 'desc')
.get()
await firestore()
.collection('chats')
.where('ownerUserId', '==', user.uid || 'chatUserId', '==', user.uid)
// .where('chatUserId', '==', user.uid)
.orderBy('createdAt', 'desc')
.get()
或者更准确地说,类似这样的内容
await firestore()
.collection('chats')
.where('ownerUserId', '==', user.uid)
.where('chatUserId', '==', user.uid)
.orderBy('createdAt', 'desc')
.get()
await firestore()
.collection('chats')
.where('ownerUserId', '==', user.uid || 'chatUserId', '==', user.uid)
// .where('chatUserId', '==', user.uid)
.orderBy('createdAt', 'desc')
.get()
对于
或
条件,我们必须执行如下多个查询:
//We define an async function that takes user uid and return chats
async function getChatsByUserOrOwner(userId) {
const chatsRef=firestore().collection('chats')
const ownerChats = chatsRef.where('ownerUserId', '==', userId).orderBy('createdAt', 'desc').get();
const userChats = chatsRef.where('chatUserId', '==', userId).orderBy('createdAt', 'desc').get();
const [ownerChatsSnapshot, userChatsSnapshot] = await Promise.all([
ownerChats,
userChats
]);
const ownerChatsList = ownerChatsSnapshot.docs;
const userChatsList = userChatsSnapshot.docs;
const allChats = ownerChatsList.concat(userChatsList);
return allChats;
}
现在我们将调用此函数以获得所需的结果:
//We call the asychronous function
getChatsByUserOrOwner(user.uid).then(result => {
result.forEach(docSnapshot => {
console.log(docSnapshot.data());
});
});