获取指向给定对象的所有解析关系

Fetch all Parse.Relations pointing to a given object

本文关键字:关系 对象 取指 获取      更新时间:2024-04-16

我使用Parse.Relation在Topic下对用户进行分组。如何检索topic.relation指向给定用户的所有主题?

问题是如何在单个调用/回调中执行此操作。

// first add() a User object to the Topic's Parse.Relation
this.friendRelation = this.topic.relation("friend");
this.friendRelation.add(user);
// save the Topic to save its newly added .relation to Parse/Mongo
this.topic.save();
// iterate by adding the same User to several Topics
// (...)
// then you want to retrieve all Parse.Relations of all Topics where that 
// specific user is pointed to
// the non-optimized way is to traverse every row in the Topics index 
// and query each topic for its relations to find matching pointers to our user, 
// which means that the number of calls is bound to the number of rows – fine for
// 10 topics in Mongo but passed 100 it won't be tolerable.

为Topic类构造一个查询,并添加一个equalTo约束。

var query = new Parse.Query(Topic);
query.equalTo("friend", user);
query.find({success: function (returnedTopics) {
    ...
    },
    error: function (...) {
    }
});

这将返回在其好友关系中包含用户的所有Topic对象。