0

I have around 30 records in collection todos, almost 25 of them contains the word work. Now when I am trying to use array-contains it doesn't return anything.

Here is my query code:

firebase.firestore().collection("todos")
.where('Workdesc', 'array-contains', 'work')
.get()
.then(querySnapshot => {
    querySnapshot.forEach(function(doc) {
        // doc.data() is never undefined for query doc snapshots
        console.log(doc.id, " ===> ", doc.data());
    });
  });

Here is my Firestore screenshot:

enter image description here

Query seems as per docs, but why is it not returning anything?

8
  • 2
    array-contains is for arrays only, it doesn't search for a string within a string Commented Mar 27, 2020 at 12:50
  • if i want to search for a word in a string, then how do i do that ? Commented Mar 27, 2020 at 14:04
  • Searching for strings within strings doesn't exist on Firestore, so you'll need to use another/additional solution for that. See the Firebase documentation here for an example: firebase.google.com/docs/firestore/solutions/search Commented Mar 27, 2020 at 14:19
  • you mean it is not possible to get results where "work" word is present ? in no way ? Commented Mar 27, 2020 at 14:21
  • Also see stackoverflow.com/questions/46568142/… (read all top answers, not just the accepted one) and more from this: google.com/search?q=firestore+query+text+contains. Commented Mar 27, 2020 at 14:22

1 Answer 1

3

You aren't getting any results, because your Workdesc property in the database is of type String and not array. See the quotation marks? If you need a property of type array, then you should save that way. See followers property, is an array. Try to save the other property in the same way.

Sign up to request clarification or add additional context in comments.

6 Comments

if i want to search for a word in a string, then how do i do that ?
Check this and this out.
I have implemented the some on a array,problem is, the value in arrayis kj implementation , now if i write kj it gives no result and if i give completely same word then only it gives the result..how can i get result by writing on kj ??
As Frank van Puffelen mentioned in his comments, there no way you can achieve that. When using an array, the match is made on the entire word (String) and not on a substring.
Hello,Answer accepted. One guidance needed, i found one answer in the above links - .startAt(searchName).endAt(searchName + "\uf8ff") , when i am using this with where like this -.where(startAt("kj").endAt("kj" + "\uf8ff")) , i am getting error expected 3 arguments,got 1, an argument of opstr was not provided, how do i solve this ?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.