在Parse中使用指针检索对象

时间:2013-12-19 19:06:03

标签: parse-platform

我有一个数据库架构,其中包括两个解析表:contacts& contactRelationships。

联系人包括联系人列表,contactRelationships将用户联系在一起以形成关系,即:

Contacts
userid: 1, username: Bob
userid: 2, username: Alice
userid: 3, username: Chris


ContactRelationships
friend_A: 1, friend_B: 2 --> Bob and Alice are friends
friend_A: 2, friend_B: 3 --> Alice and Chris are friends

我想构建一个查询,例如:“SELECT * FROM ContactRelationships WHERE friend_A = 1 AND friend_B = 2”。

我现在遇到的问题是,在Parse中,我将指针存储到联系人,我正在尝试构建查询,但我没有指针存储在本地数据(核心数据)中。我只存储了用户ID。

有没有办法做到这一点,而无需更改数据库来存储用户ID字符串而不是指针?

1 个答案:

答案 0 :(得分:4)

也许这个答案可以帮助您查询:Parse.com: Find all objects belonging to a user with objectId

基本上,您应该将查询设置为包含嵌套对象,而不是仅带指针。您可以创建所需的用户对象并比较整个对象而不仅仅是对象ID。

我不知道您编写的是哪种语言,但在Cloud Code(Javascript)中,代码可能如下所示:

// Create your query and set which nested objects you want
var query = new Parse.Query('ContactRelationships');
query.include('friend_A');
query.include('friend_B');

// Create local instances of your user objects
var userA = new Parse.User();
userA.id = 1;
var userB = new Parse.User();
userB.id = 2;

// Query your database comparing objects
query.equalTo('friend_A', fetchedUserA);
query.equalTo('friend_B', fetchedUserB);
query.find({
    success: function(result) {
        // Do your thing here
    }
});