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?

halfer
  • 19,824
  • 17
  • 99
  • 186
user2828442
  • 2,415
  • 7
  • 57
  • 105
  • 2
    `array-contains` is for arrays only, it doesn't search for a string within a string – Blundering Philosopher Mar 27 '20 at 12:50
  • if i want to search for a word in a string, then how do i do that ? – user2828442 Mar 27 '20 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: https://firebase.google.com/docs/firestore/solutions/search – Frank van Puffelen Mar 27 '20 at 14:19
  • you mean it is not possible to get results where "work" word is present ? in no way ? – user2828442 Mar 27 '20 at 14:21
  • Also see https://stackoverflow.com/questions/46568142/google-firestore-query-on-substring-of-a-property-value-text-search (read all top answers, not just the accepted one) and more from this: https://www.google.com/search?q=firestore+query+text+contains. – Frank van Puffelen Mar 27 '20 at 14:22
  • i read this before but i didnt understood how `collectionRef.where('name', '>=', 'bar').where('name', '<=', 'foo')` works ? When i tried with `>` and word `work` , strings which didnt had this word also came....why ? – user2828442 Mar 27 '20 at 14:27
  • 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` ?? – user2828442 Mar 27 '20 at 14:49
  • Hi user2828442. I've edited a lot of chatty material from your questions in the past, and you will have received the notifications from the Stack Overflow platform. Some of your material has a needy, begging quality, and suggests you think that displays of helplessness will cause people to pity your situation and/or feel coerced into the social obligation of assisting the less fortunate. This is not appropriate behaviour for a volunteer platform. Please stick to technical writing. – halfer Mar 27 '20 at 16:33

1 Answers1

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.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • if i want to search for a word in a string, then how do i do that ? – user2828442 Mar 27 '20 at 14:04
  • Check **[this](https://stackoverflow.com/a/47133345/5246885)** and **[this](https://stackoverflow.com/a/52627798/5246885)** out. – Alex Mamo Mar 27 '20 at 14:21
  • 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` ?? – user2828442 Mar 27 '20 at 14:48
  • 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. – Alex Mamo Mar 27 '20 at 15:04
  • 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 ? – user2828442 Mar 28 '20 at 04:58
  • I'm not familiar with this Exception. I recommend you post a new question, so you can get other opinions also from other Firebase developers. – Alex Mamo Mar 28 '20 at 11:22