2012-07-27 71 views
2

我正在使用Parse.Relation在主題下對用戶進行分組。如何檢索指向給定用戶的所有主題(topic.relation)?獲取指向給定對象的所有Parse.Relations

問題是關於在單個調用/回調中執行此操作。

// 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. 
+2

請發佈一些代碼,這將有助於我們更清楚地理解您的問題。 – 2012-07-27 02:34:58

+0

嗨謝赫,這是一些初始代碼來設置多對多的關係。現在製作迭代器給你一個效率低下的想法。 – 2012-07-27 18:04:23

回答

5

構造對Topic類的查詢,並添加equalTo約束。

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

這將返回包含其朋友關係中的用戶的所有Topic對象。

+0

是的,真棒,謝謝!基本上,如果我理解正確,一旦使用'Topic'上的'.relation()'構建關係索引,我可以以正常方式查詢它,其中關鍵字是關係的名稱。 – 2012-07-27 21:57:19

+0

我個人發現「父母」和「包含」功能的概念也很有用:http://stackoverflow.com/a/22369032/470749 – Ryan 2014-03-27 17:12:13

+0

這仍然支持嗎?我在使用SDK 1.2.17時遇到了問題。 – wberry 2014-05-25 17:59:55