grahamearley / FirestoreGoogleAppsScript

A Google Apps Script library for accessing Google Cloud Firestore.
http://grahamearley.website/blog/2017/10/18/firestore-in-google-apps-script.html
MIT License
648 stars 109 forks source link

Difficulties with 'contains' operator #125

Closed DF916 closed 3 years ago

DF916 commented 3 years ago

Hi everyone! First of all, I'd like to thank everyone involved in the development of this tool for their time and effort, both coding and helping those of us who are working with it when we have questions or issues.

That being said, and before getting to the template offered - which I understand will probably be the most useful format for developers to get the point - I'd like to offer a bit of context. I'm using this app to conect a database I have hosted in Firebase with a Google Spreadsheet table, which I'm using as a simple and pretty basic interface for showing data. This database contains several collections, each one with multiple documents. As for the documents, they have different fields, most of which are arrays.

And here comes the problem. When I query with an "==" operator, I do get the data I'm looking for. But, when I query with the "contains" operator, all I get is an empty array. The data format of the field I'm currently querying is "1234 - Surname Name", a string that combines a number id and a name. There might be more than one person identified, which would result in a field looking like "1234 - Surname1 Name1, 5678 - Surname2 Name2". When I query the database with the "==" operator, I get the documents with fields that contain just one name, so the connection between my Google Script and my FB database is working properly. But I need to get ALL documents in which the queried person has been identified, both alone and with someone else - so the "==" operator is not what I'm looking for.

And yet, when I use the "contains" operator, all I get, as I said, is an empty array. I've tried everything: querying the database with just the number, with just the name, or the surname, or just a single letter. Nothing has worked. Over and over again, I get an empty array. Even when I query the database with the complete, exact string that works with the "==" operator.

I'm pretty sure it has to be something actually simple and obvious, but it has come to a point in which I'm too absorbed to realize what is it. I was hoping to get some fresh perspective!

Snippet of code: const firestore = FirestoreApp.getFirestore(email, key, projectId); var param_query = firestore.query("Collection").Where("field", "contains", "1234 - Surname Name").Execute() (I'm not providing the exact data, just an example of the code being used. Collection, field, and 1234 - Surname Name are generic names)

Further explanations I hope the introduction provided is self-explicatory. Here I can provide a more schematic approach.

  1. Connection with Firebase through the FirestoreApp properly working, since I can retrieve full collections or even query the database with "==" operator
  2. Query with "contains" operator not working. There is no error, but all I get is an empty array. No matter how vague or precise the argument provided, from a single letter to a whole string, the query always returns []

Additional comments Just wanted to apologize in advance for any possible lack of information, or misunderstanding I might cause, for not being clear enough in the explanations. If such is the case, please let me know and I'll try to explain as thoroughly as possible. Would like to thank in advance, as well, for your time and attention!

Library Version: 33

LaughDonor commented 3 years ago

Thank you very much for your interest in this library!

I have ensured that we have Test Cases covering every scenario, so that everyone can reference the test data, for examples of usage since it may not always be straightforward looking.

In your case, the "contains" operator would be equivalent to the following Javascript code if these items were first-class JS objects:

collection.filter(doc => doc[field].contains("1234 - Surname Name"))

and so the "field" should be an Array where one of the elements could be "1234 - Surname Name".

I hope this makes things clear.