如何根据相关模型排除环回结果

时间:2022-10-15 23:02:09

I am using mongo connector.

我正在使用mongo连接器。

I have a offers model which has a hasMany relation with my Users model via interested_users key. Basically the idea is that an user can mark an offer as interested or not_interested, and I need to exclude the not_interested offers. What is the best/efficient way to achieve this?

我有一个offer模型,它通过interested_users键与我的Users模型有很多关系。基本上,这个想法是用户可以将要约标记为感兴趣或不感兴趣,我需要排除不感兴趣的要约。实现这一目标的最佳/有效方法是什么?

1 个解决方案

#1


0  

I am still not sure I fully understand your issue, but it is definitely related to querying data with loopback REST API, and especially where filter

我仍然不确定我是否完全理解您的问题,但它肯定与使用loopback REST API查询数据有关,尤其是在过滤器中

If your model offer has a property someone_is_interested (boolean), then the following request could work for you

如果您的模型商品具有属性someone_is_interested(boolean),则以下请求可能对您有效

GET /api/offers/?filter[where][someone_is_interested]=true

But I have the feeling you want to use the relation to store all users that are interested in an offer. In this case, simply establish the relation between any interested user and the offer with this request (in the database, one instance of offer with id=1, and a user with id=2:

但我觉得您希望使用该关系来存储对商品感兴趣的所有用户。在这种情况下,只需在此请求中建立任何感兴趣的用户和商品之间的关系(在数据库中,id为1的商品的一个实例,以及id = 2的用户:

PUT /api/offers/1/interested_users/rel/2

And to remove the relation (just the relation, not the user or offer)

并删除关系(只是关系,而不是用户或提议)

DELETE /api/offers/1/interested_users/rel/2

Then, you can simply query all related users of offer with id=1, which will give you all interested users on that offer

然后,您可以简单地查询id = 1的所有相关的商品用户,这将为您提供该商品的所有感兴趣的用户

GET /api/offers/1/interested_users

#1


0  

I am still not sure I fully understand your issue, but it is definitely related to querying data with loopback REST API, and especially where filter

我仍然不确定我是否完全理解您的问题,但它肯定与使用loopback REST API查询数据有关,尤其是在过滤器中

If your model offer has a property someone_is_interested (boolean), then the following request could work for you

如果您的模型商品具有属性someone_is_interested(boolean),则以下请求可能对您有效

GET /api/offers/?filter[where][someone_is_interested]=true

But I have the feeling you want to use the relation to store all users that are interested in an offer. In this case, simply establish the relation between any interested user and the offer with this request (in the database, one instance of offer with id=1, and a user with id=2:

但我觉得您希望使用该关系来存储对商品感兴趣的所有用户。在这种情况下,只需在此请求中建立任何感兴趣的用户和商品之间的关系(在数据库中,id为1的商品的一个实例,以及id = 2的用户:

PUT /api/offers/1/interested_users/rel/2

And to remove the relation (just the relation, not the user or offer)

并删除关系(只是关系,而不是用户或提议)

DELETE /api/offers/1/interested_users/rel/2

Then, you can simply query all related users of offer with id=1, which will give you all interested users on that offer

然后,您可以简单地查询id = 1的所有相关的商品用户,这将为您提供该商品的所有感兴趣的用户

GET /api/offers/1/interested_users