Abd*_*lla 1 firebase flutter google-cloud-firestore
我使用 Firebase cloud-firestore 使用此代码检查用户名是否唯一或存在于 Firestore 云中,并且到目前为止它可以正常工作,但是如果我使用此代码检查超过 10000 个用户名会发生什么?
那么检查数据库需要多少时间呢?您是否建议改用 algolia 或 elasticSearch 。
final QuerySnapshot result = await Future.value(Firestore.instance
.collection('check_username')
.where('username', isEqualTo: userNameController.text.toLowerCase())
.limit(1)
.getDocuments());
final List<DocumentSnapshot> documents = result.documents;
if (documents.length == 1) {
print("UserName Already Exits");
setState(() {
_userExist = documents.length == 1;
});
} else {
print("UserName is Available");
setState(() {
_userExist = documents.length == 1;
});
}```
Run Code Online (Sandbox Code Playgroud)
Firestore 的一个有趣(且非常独特)的技巧是读取操作的性能不依赖于集合中的文档数量。相反,它仅取决于您读取的数据量。
因此,在您的查询中,您正在检索单个文档:
final QuerySnapshot result = await Future.value(Firestore.instance
.collection('check_username')
.where('username', isEqualTo: userNameController.text.toLowerCase())
.limit(1)
Run Code Online (Sandbox Code Playgroud)
对于性能而言,无论是 100 个文档check_username、100,000 个文档还是 100,000,000 个文档,性能总是相同的。
也就是说,我建议使用用户名本身作为此集合中的键。这会自动确保每个用户名只能有一个文档,因为文档 ID 在其集合中必须是唯一的。
因此,如果您使用用户名作为文档 ID,则检查变为:
final DocumentSnapshot result = await Future.value(Firestore.instance
.collection('check_username')
.document(userNameController.text.toLowerCase())
.get());
if (result.exists) {
print("UserName Already Exits");
} else {
print("UserName is Available");
}
setState(() {
_userExist = result.exists;
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
828 次 |
| 最近记录: |