This is a migrated thread and some comments may be shown as answers.

check if id part of multiple relation

3 Answers 57 Views
Cloud Code
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Max
Top achievements
Rank 1
Max asked on 18 Mar 2015, 03:39 PM
i have a multiple relation defined in my backend service, where i now want to check if the relation exists, thus the id is part of the array.

however, i can not find any solution that only provides me with the elements where the specified object is part of the relation.
i did search around and found the "$elemMatch" query type of mongoDB, but it seems that it is currently not supported?

therefore my current approach would be to get all object and check each relation via indexOf and discard any that i do not need/want.
but somehow this feels wrong to me, is there any other approach or solution that i currently overlook?

the only other solution i can think of is to define the relation in both ways and have it the other way around, but i kinda want to mitigate the need to keep it synchron and always update both relations

3 Answers, 1 is accepted

Sort by
0
Accepted
Vasil
Telerik team
answered on 19 Mar 2015, 09:25 AM
Hello Max,

If I understand your question correctly you want to get an item which has a multiple relation to a content type and want the objects which are related to it. We have added the X-Everlive-Expand header exactly for cases like this one. In order to receive an array of the actual items, instead of the ids you need to add the "X-Everlive-Expand" header to your GET query with a value similar to this:
{
"your-multiple-relation-field" : true
}
This will return the related items, instead of only the ids, however this is the simplest possible syntax for the header. You can also apply filtering, sorting, projections on the relations - for details you can take a look at the documentation for relations: http://docs.telerik.com/platform/backend-services/features/data-storage/relations/introduction.

A simple example using the Javascript SDK would be:

var query = new Everlive.Query();
var expandExpression = {
"your-multiple-relation-field": true
};

query.expand(expandExpression);
el.data('your-content-type-name').get(query, successCallback, errorCallback);

In the example you have to change the "your-content-type-nameto the your content type name and "your-multiple-relation-fieldto the field which holds the relations and provide a successCallback and errorCallback as you normally would.

On a side note - you can find documentation about how to use the $elemMatch in this article - http://docs.telerik.com/platform/backend-services/development/rest-api/querying/mongodb-operators.

Let me know if you have questions.

Regards,
Vasil
Telerik
 
Everlive is now Telerik Backend Services, and is part of the Telerik Platform.
 
0
Max
Top achievements
Rank 1
answered on 22 Mar 2015, 03:17 PM
turns out i was using the wrong syntax and it works as expected.

i did already know about expand expressions, but these are not necessary here, as i (only) have the Id that i want to filter by.
0
Vasil
Telerik team
answered on 24 Mar 2015, 04:09 PM
Hello Max,

Glad to hear you got the issue sorted out! Please do not hesitate to contact us should you have any further queries.

Regards,
Vasil
Telerik
 
Everlive is now Telerik Backend Services, and is part of the Telerik Platform.
 
Tags
Cloud Code
Asked by
Max
Top achievements
Rank 1
Answers by
Vasil
Telerik team
Max
Top achievements
Rank 1
Share this question
or