check if id part of multiple relation

4 posts, 1 answers
  1. Max
    Max avatar
    19 posts
    Member since:
    Jan 2015

    Posted 18 Mar 2015 Link to this post

    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
  2. Answer
    Vasil
    Admin
    Vasil avatar
    19 posts

    Posted 19 Mar 2015 Link to this post

    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.
     
  3. Max
    Max avatar
    19 posts
    Member since:
    Jan 2015

    Posted 22 Mar 2015 in reply to Vasil Link to this post

    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.
  4. Vasil
    Admin
    Vasil avatar
    19 posts

    Posted 24 Mar 2015 Link to this post

    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.
     
Back to Top