I have a following collections
-likes (collection)
-{uid} (document)
{otheruserUID: true, anotherUID: true ...}
-likedBy (collection)
-{uid} (document
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