【发布时间】:2020-02-06 23:21:07
【问题描述】:
如问题所述,Firebase 每个查询仅允许一个数组包含任意 (https://firebase.google.com/docs/firestore/query-data/queries)。
当有人需要复杂查询时会发生什么?我的查询需要大约 10 个 .where() 和 4 个 array-contains-any 子句。是否有解决方法,或者我必须从第一个 array-contains-any 中获取结果,然后一次又一次地标注?似乎很费力而且没有必要。
var usersMatchesCollection = config.db.collection("Users");
var currentUserMatchesAllData = usersMatchesCollection.where('preferences.lookingFor', 'array-contains-any', docDataPreferences.lookingFor)
.where('info.identifyAs', '==', docDataPreferences.prefIdentifyAs)
.where('info.languages', 'array-contains-any', docDataPreferences.prefLanguages)
.where('age', 'array-contains-any', docDataPreferences.ageRange)
.where('profession', 'array-contains-any', docDataPreferences.prefProfession)
.where('education', '==', docDataPreferences.prefEducation)
.where('kids', '==', docDataPreferences.prefKids)
.where('married', '==', docDataPreferences.prefMarried)
.where('drinking', '==', docDataPreferences.prefDrinking)
.where('smokingCig', '==', docDataPreferences.prefSmokingCig)
await currentUserMatchesAllData.get().then( function (matchesQuerySnapshot) {
matchesQuerySnapshot.forEach(function(doc) {
console.log('doc data: ' + JSON.stringify(doc.data().id));
})
})
【问题讨论】:
-
看起来您可能对 Firestore 的查询功能提出了一些要求。如果您无法对数据进行建模,以便在考虑到限制的情况下对其进行查询,那么您可能必须按照您所指出的那样进行多次查询。
-
如果您想将其发布为答案,我可以让它与此评论一起使用。不幸的是,没有一种不那么费力的查询方式,希望有人会看到这一点并让我们知道另一种方式!
标签: firebase react-native google-cloud-firestore