How to query for matches between users in cloud firestore?

前端 未结 1 1858
花落未央
花落未央 2021-01-07 10:30

I have a following collections

-likes (collection)
  -{uid} (document)
   {otheruserUID: true, anotherUID: true ...}
-likedBy (collection)
  -{uid} (document         


        
相关标签:
1条回答
  • 2021-01-07 10:49

    Personally, I would simply have a single collection, called likes. Each like generates a new document with an auto-id and contains 3 fields: user (an object containing the id and name of the user), likedBy (an object containing the id and name of the user who liked them) and timestamp (when they were liked).

    You'll be able to carry out the following queries:

    // Find all users who liked likedUser, sorted by user
    db.collection('likes').where('likedBy.name', '!=', null).where('user.id', '==', likedUser).orderBy('likedBy.name');
    
    // Find all users who were liked by likedByUser, sorted by user
    db.collection('likes').where('user.name', '!=', null).where('likedBy.id', '==', likedByUser).orderBy('user.name');
    

    The first time that you run these queries, you will get an error, telling you to create an index. This error will include the URL to create the index for you.

    The first where is required to allow the orderBy to work, see the documentation section Range filter and orderBy on the same field

    0 讨论(0)
提交回复
热议问题