0

I would want to exclude some documents from a GET request, so I have written the following code (note the line users = users.where(admin.firestore.FieldPath.documentId(), '!=', k);):

let users = admin_firestore.collection('users');
const likes = admin_firestore.collection('likes_of_users_posts');
likes.get().then(function(likes_docs) {
        const map_users_id_with_number_of_likes = [];
        likes_docs.forEach(like_doc => {
            if(!(like_doc.data().post_owner in map_users_id_with_number_of_likes)) {
                map_users_id_with_number_of_likes[like_doc.data().post_owner] = 0;
            }
            map_users_id_with_number_of_likes[like_doc.data().post_owner] += 1;
        });
        Object.keys(map_users_id_with_number_of_likes).forEach((k) => {
            users = users.where(admin.firestore.FieldPath.documentId(), '!=', k);
        });

It's part of my Google Cloud Functions function.

When, in the Android app, I execute this Cloud Function function, I can see the following error, in the logs:

Error: Value for argument "opStr" is invalid. Acceptable values are: <, <=, ==, >, >=, array-contains

However I really need towrite something equivalent to: "different than X AND different than Y AND...". So to chain the .where(admin.firestore.FieldPath.documentId(), '!=', k) as I did.

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
JarsOfJam-Scheduler
  • 2,809
  • 3
  • 31
  • 70

1 Answers1

2

No there isn't a != operator for Firestore queries. The reason for that is the way the database is indexed, see this official video for more info.

There are some workarounds, for example, combining a query with the > operator and another one with the < one. This article shows how to combined two different queries in order to simulate an OR query.

I am not sure if this will be possible in your case, as you mention "different than X AND different than Y AND...". You should probably use a mix of the technique described in the article to simulate an OR query and remove some records in the front end.

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
  • Yes it won't be possible. Too bad, I have to change the logic of my code to solve the pb – JarsOfJam-Scheduler Apr 16 '20 at 09:51
  • I have to exclude documents from my GET requests because I want to affect an arbitrary value to a field of these documents, while I will initialize to 0 this field of the other documents. (only one collection is concerned). Do you think I can use batch updates to initialize this field of all documents to 0, and then use batch updates to affect the arbitrary value to the field of some documents, and then commit this batch? So the order of these updates must be respected, else it won't work (in this case, this fields of the documents with a value normally != 0 would be 0 and it's bad). – JarsOfJam-Scheduler Apr 16 '20 at 11:09
  • That would work, but it shall be two different batches, see your [question](https://stackoverflow.com/questions/61233322/firebase-firestore-batch-commits-are-the-batchs-instructions-executed-in-the-s/61233448#61233448) of yesterday. – Renaud Tarnec Apr 16 '20 at 13:59