Updating Data Items

Updating Data Items

The Backend Services JavaScript SDK allows you to update single or multiple data items in a single method call by specifying an ID field or by passing a filter expression. Instead of passing a plain object for filter, you can pass an instance of the Everlive.Query type.

Updating a Single Item by ID

To update an item, pass its ID to the data.updateSingle() method.

var el = new Everlive('your-app-id');
var data = el.data('type-name');
data.updateSingle({ Id: 'item-id', 'Author': 'Harper Lee' },
    function(data){
        alert(JSON.stringify(data));
    },
    function(error){
        alert(JSON.stringify(error));
    });

You can update system fields such as CreatedAt and CreatedBy. However, you can set them only after doing the following:

Updating Multiple Items

Multiple items are typically updated trough a filter but you can also omit it in which case you will be updating all items in the content type.

Always double-check your filter expression and test it on a dummy copy of your data. This will help you avoid overwriting the wrong items.

The following example updates all items that contain a Author field valued "Sample Text". On success, the response contains the number of updated items.

var el = new Everlive('your-app-id');
var data = el.data('type-name');
data.update({ 'Author': 'Updated Sample Text' }, // update data
    { 'Author': 'Sample Text' }, // filter expression
    function(data){
        alert(JSON.stringify(data));
    },
    function(error){
        alert(JSON.stringify(error));
    });

Be careful when updating fields marked as unique (IsUnique) by filter as it may match multiple items. In this case, Telerik Platform updates the first matching item if the update value does not duplicate an existing item, after which it stops executing the request and returns an error. The rest of the matching items are not updated.

Using MongoDB Operators

You can use native MongoDB update operators for additional flexibility of the update operation. This is accomplished by passing the update expression as a parameter to the rawUpdate method. See the next subsections for usage examples.

Incrementing a Number Field

The following snippet increments the AvailableCount field of the item matching the specified filter.

var el = new Everlive('your-app-id');
var data = el.data('type-name');

var attributes = {
    '$inc': {
        'AvailableCount': 1
    }
};

var filter = {
    'Id': 'item-id'
};

data.rawUpdate(attributes, filter,
    function (data) {
        alert(JSON.stringify(data));
    },
    function (error) {
        alert(JSON.stringify(error));
    });

Updating an Array Field

The following snippet adds a new user ID to the Likes field of the item matching the specified filter.

var el = new Everlive('your-app-id');
var data = el.data('Activities');

var likedUserId = 'user-id';

// "$push" adds an item to an array.
// "$addToSet" adds elements to an array only if they do not already exist in the set.
var attributes = {
    "$push": {
        "Likes": liked-user-id
    }
};

var filter = {
    'Id': "item-id"
};

data.rawUpdate(attributes, filter, function (data) {
    console.log(JSON.stringify(data));
}, function (error) {
    console.log(JSON.stringify(error));
});

The following snippet updates the first value in the Notes array that matches the specified filter.

var el = new Everlive('your-app-id');
var data = el.data('Activities');

// the 'Notes' field is an Array
var filter = {
    'Notes': "old value"
};

// update the first element in the array 'Notes' that equals 'old value', to 'new value'
var attributes = {
    "$set": {
        "Notes.$": "new value"
    }
};

data.rawUpdate(attributes, filter, function(data) {
    console.log(JSON.stringify(data));
}, function(error) {
    console.log(JSON.stringify(error));
});

See Also

Start a free trial Request a demo
Contact us: +1-888-365-2779
sales@telerik.com
Copyright © 2016-2017, Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved.