Firestore query documents startsWith a string

2020-01-27 03:50发布

Is it possible to query a firestore collection to get all document that starts with a specific string?

I have gone through the documentation but do not find any suitable query for this.

4条回答
贪生不怕死
2楼-- · 2020-01-27 04:01

same as answered by Gil Gilbert. Just an enhancement and some sample code. use String.fromCharCode and 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.

Warning: all search criteria in firestore is CASE SENSITIVE.

查看更多
等我变得足够好
3楼-- · 2020-01-27 04:05

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:

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.

查看更多
甜甜的少女心
4楼-- · 2020-01-27 04:10

Extending the previous answers with a shorter version:

  const text = 'start with text here';
  const end = text.replace(/.$/, c => String.fromCharCode(c.charCodeAt(0) + 1));

  query
    .where('stringField', '>=', text)
    .where('stringField', '<', end);

IRL example

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;
}
查看更多
来,给爷笑一个
5楼-- · 2020-01-27 04:10

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()
查看更多
登录 后发表回答