如何在Cloud Firestore中查询用户之间的匹配? [英] How to query for matches between users in cloud firestore?

查看:74
本文介绍了如何在Cloud Firestore中查询用户之间的匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下收藏集

-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?

推荐答案

就我个人而言,我只是拥有一个名为likes的集合.每个人都会生成一个带有自动ID的新文档,并包含3个字段:user(包含用户idname的对象),likedBy(包含id表示喜欢的用户)和timestamp(表示喜欢).

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');

首次运行这些查询时,会出现错误,告诉您创建索引.该错误将包含用于为您创建索引的URL.

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.

第一个where是必需的,以允许orderBy工作,请参见文档部分

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

这篇关于如何在Cloud Firestore中查询用户之间的匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆