[英]Firestore query documents startsWith a string
Is it possible to query a firestore collection to get all document that starts with a specific string?是否可以查询 firestore 集合以获取以特定字符串开头的所有文档?
I have gone through the documentation but do not find any suitable query for this.我已经阅读了文档,但没有找到任何合适的查询。
You can but it's tricky.你可以,但它很棘手。 You need to search for documents greater than or equal to the string you want and less than a successor key.
您需要搜索大于或等于所需字符串且小于后继键的文档。
For example, to find documents containing a field 'foo'
staring with 'bar'
you would query:例如,要查找包含字段的文档
'foo'
与盯着'bar'
你会质疑:
db.collection(c)
.where('foo', '>=', 'bar')
.where('foo', '<', 'bas');
This is actually a technique we use in the client implementation for scanning collections of documents matching a path.这实际上是我们在客户端实现中使用的一种技术,用于扫描与路径匹配的文档集合。 Our successor key computation is called by a scanner which is looking for all keys starting with the current user id.
我们的后继密钥计算由扫描器调用,该扫描器正在查找以当前用户 ID 开头的所有密钥。
same as answered by Gil Gilbert.与吉尔吉尔伯特的回答相同。 Just an enhancement and some sample code.
只是一个增强和一些示例代码。 use String.fromCharCode and String.charCodeAt
使用String.fromCharCode和String.charCodeAt
var strSearch = "start with text here";
var strlength = strSearch.length;
var strFrontCode = strSearch.slice(0, strlength-1);
var strEndCode = strSearch.slice(strlength-1, strSearch.length);
var startcode = strSearch;
var endcode= strFrontCode + String.fromCharCode(strEndCode.charCodeAt(0) + 1);
then filter code like below.然后过滤代码如下。
db.collection(c)
.where('foo', '>=', startcode)
.where('foo', '<', endcode);
Works on any Language and any Unicode.适用于任何语言和任何 Unicode。
Warning: all search criteria in firestore is CASE SENSITIVE.警告:firestore 中的所有搜索条件都区分大小写。
const text = 'start with text here';
const end = text.replace(/.$/, c => String.fromCharCode(c.charCodeAt(0) + 1));
query
.where('stringField', '>=', text)
.where('stringField', '<', end);
async function search(startsWith = '') {
let query = firestore.collection(COLLECTION.CLIENTS);
if (startsWith) {
const end = startsWith.replace(
/.$/, c => String.fromCharCode(c.charCodeAt(0) + 1),
);
query = query
.where('firstName', '>=', startsWith)
.where('firstName', '<', end);
}
const result = await query
.orderBy('firstName')
.get();
return result;
}
Credit to the java answer by Kyo归功于 Kyo的java 答案
final strFrontCode = term.substring(0, term.length - 1);
final strEndCode = term.characters.last;
final limit =
strFrontCode + String.fromCharCode(strEndCode.codeUnitAt(0) + 1);
final snap = await FirebaseFirestore.instance
.collection('someCollection')
.where('someField', isGreaterThanOrEqualTo: term)
.where('someField', isLessThan: limit)
.get();
I found this, which works perfectly for startsWith
我找到了这个,它非常适合
startsWith
const q = query(
collection(firebaseApp.db, 'capturedPhotos'),
where('name', '>=', name),
where('name', '<=', name + '\uf8ff')
)
The above are correct!以上都是正确的! Just wanted to give an updated answer!
只是想给出一个更新的答案!
var end = s[s.length-1]
val newEnding = ++end
var newString = s
newString.dropLast(1)
newString += newEnding
query
.whereGreaterThanOrEqualTo(key, s)
.whereLessThan(key, newString)
.get()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.