Updating Array Data Type

3 posts, 2 answers
  1. Kelly
    Kelly avatar
    73 posts
    Member since:
    Apr 2011

    Posted 12 Feb 2014 Link to this post

    Is it possible to update an Array type field using REST?  For example, can one add an object to an Array field in one operation?

    Thank you,

    Kelly
  2. Answer
    Anton Dobrev
    Admin
    Anton Dobrev avatar
    539 posts

    Posted 13 Feb 2014 Link to this post

    Hello Kelly,

    Thanks for your question!

    Yes, just refer to the following code (note the $push operator):
    var object = {
        $push: {
            myFieldName: "guid-or-object-here"
        }
    };
    $.ajax({
        type: "PUT",
        headers: {
            "Authorization": "MasterKey your-master-key-here"
        },
        contentType: "application/json",
        data: JSON.stringify(object),
        success: function (data) {
            alert(JSON.stringify(data));
        },
        error: function (error) {
            alert(JSON.stringify(error));
        }
    });

    Also, you can add several items in the array field using the $pushAll operator:
    var object = {
        $pushAll: {
            myFieldName: [ "item-here", "", ... ]
        }
    };

    Or, add an item only if it does not exist, using the $addToSet operator:
    var object= {
        $addToSet: {
            myFieldName: "item-id-here"
        }
    };

    Please, let us know if this answers your question.

    Regards,
    Anton Dobrev
    Telerik
    Everlive is now Telerik Backend Services, and is part of the Telerik Platform. For more information on the new name, and to learn more about the Platform, register for the free online keynote and webinar on Wednesday, February 12, 2014 at 11:00 a.m. ET (8:00 a.m. PT)
  3. Answer
    Anton Dobrev
    Admin
    Anton Dobrev avatar
    539 posts

    Posted 13 Feb 2014 Link to this post

    Hello,

    Just to mention that the 'MasterKey' of your app should not be exposed on the client-side or distributed with your app.
    Instead, a header authenticating the current user must be used in the client code:
    headers: { "Authorization" : "Bearer current-user-access-token-here" }

    Regards,
    Anton Dobrev
    Telerik
    Everlive is now Telerik Backend Services, and is part of the Telerik Platform. For more information on the new name, and to learn more about the Platform, register for the free online keynote and webinar on Wednesday, February 12, 2014 at 11:00 a.m. ET (8:00 a.m. PT)
Back to Top