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

Updating Array Data Type

2 Answers 108 Views
General Discussion
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Kelly
Top achievements
Rank 1
Kelly asked on 12 Feb 2014, 10:41 PM
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 Answers, 1 is accepted

Sort by
0
Accepted
Anton Dobrev
Telerik team
answered on 13 Feb 2014, 12:55 PM
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)
0
Accepted
Anton Dobrev
Telerik team
answered on 13 Feb 2014, 02:33 PM
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)
Tags
General Discussion
Asked by
Kelly
Top achievements
Rank 1
Answers by
Anton Dobrev
Telerik team
Share this question
or