2017-10-04 65 views

回答

9

你可以但它很棘手。您需要搜索大于或等于所需字符串且小于后继密钥的文档。

例如,要查找包含的字段'foo''bar'盯着你查询文件:

db.collection(c) 
    .where('foo', '>=', 'bar') 
    .where('foo', '<', 'bas'); 

这实际上是我们在匹配的路径文件扫描集合中的客户端实现中使用的技术。我们的successor key computationscanner调用,它正在查找以当前用户标识开头的所有密钥。

+0

继任钥匙的用途是什么? –

+0

后继键限制查询,使得结果仅包含以特定字符串开头的字符串。没有它搜索只是foo> ='酒吧'也将返回'动物园',因为该字符串也是按字典顺序大于酒吧。 –

6

与Gil Gilbert的回答相同。 只是一个增强和一些示例代码。 使用String.fromCharCodeString.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); 

然后像下面过滤的代码。

db.collection(c) 
.where('foo', '>=', startcode) 
.where('foo', '<', endcode); 

适用于任何语言和任何Unicode。

警告:firestore中的所有搜索条件都是CASE SENSITIVE。

+0

是否有可能在同一个文档中执行这两个或多个字段。例如:where('foo','> =',startcode).where('foo','<',endcode).where('bar','<',endcode).where('bar,'< ',endcode);' – Erik

+0

也许这会击中[此外,您只能在单个字段上执行范围比较(<, <=, >,> =)](https://firebase.google.com/docs/firestore/query-data /查询) – Erik