0

I have a two firebase collection "user" and "schools"

User 
-> firstName
-> lastName
-> friends []-> array of reference of User Id
-> schoolRef -> reference of school

Now, I want to fetch two friends documents and two non friends documents from users collection by Passing UserId and SchoolRef Id but not able to form the query correctly.

I am fetching 4 options i.e. 4 Users from the Users Collections. 2 are friends, 2 are strangers but they all belong to same school.

export const onGetUserOptions = functions.https // }) //   enforceAppCheck: true, // .runWith({
  .onCall(async (data, context) => {
   
    const MAX_TOTAL_USER_OPTION = 4;
    const MAX_FRIENDS_OPTION = 2;

    var db = admin.firestore();
    var users = db.collection('users');

    const userId = data.uid;
    const schoolRef = data.schoolRef;

    //fetch users document where userId reference exists in friends array
    var friends = await users
      .where('friends', 'array-contains', userId)
      .limit(MAX_FRIENDS_OPTION)
      .get();


    const friendsCount = friends.docs.length;

    //fetch list of non friends where uid does not exist in friends array, excluding self also
    var nonFriends = await users
      .where('uid', '!=', userId)
      .where('friends', 'not-in', '/users/' + userId)
      .where('schoolRef', '==', schoolRef)
      .limit(MAX_TOTAL_USER_OPTION - friendsCount)
      .get();

    console.log('Non Friends: ', nonFriends.docs.toString());

    //merge friends and non friends and return
    const options: admin.firestore.QueryDocumentSnapshot<admin.firestore.DocumentData>[] =
      [];
    friends.forEach((friend) => {
      options.push(friend);
    });
    nonFriends.forEach((nonFriend) => {
      options.push(nonFriend);
    });

    return {
      code: 200,
      status: 'success',
      message: 'User options fetched successfully',
      data: options,
    };
  });
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Rishabh
  • 75
  • 7

1 Answers1

0

This condition in your second query seems off:

.where('friends', 'not-in', '/users/' + userId)

If you look at the documentation for not-in condition, they're supposed to look like this:

citiesRef.where('country', 'not-in', ['USA', 'Japan'])

I'm not sure I understand your use-case for the second query, but it sounds like you might be looking for a array-not-contains like condition, which doesn't exist on Firestore. For more on this, see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I have removed the 2nd query but my first query itself is also not working. I guess I am not able to pass userId to array of reference users correctly. Kindly guide there. – Rishabh Jun 14 '23 at 13:41
  • First off: does the explanation in my answer (and the links) make sense, and explain why you second query can't work? If so, see [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers). – Frank van Puffelen Jun 15 '23 at 05:02
  • Yes, that is why I removed it. Thank you!. But can you help with the first query. – Rishabh Jun 15 '23 at 05:04
  • Please limit post to a single problem. Since I see that you posted a second questions about the second problem, also see [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – Frank van Puffelen Jun 15 '23 at 05:13