Reputation: 7580
I have a following collections
-likes (collection)
-{uid} (document)
{otheruserUID: true, anotherUID: true ...}
-likedBy (collection)
-{uid} (document)
{otheruserUID: true, anotherUID: true ...}
A user can like other users. What I want to query for is given a user, query for all matches of that user. Should I query whole likes and likedby data and run match in result and produce match results? Is there any other easy way to do this? Or may be better way to model the data?
Upvotes: 1
Views: 781
Reputation: 4898
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
Upvotes: 3