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

Updating multiple rows with rawUpdate

1 Answer 53 Views
JavaScript SDK
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Nikola
Top achievements
Rank 1
Nikola asked on 16 May 2016, 03:14 PM

Hello,

I am trying to understand how rawUpdate filter works.

 

I have extended the Users content type to have a property Followers which is a multiple reference to the Users table. I have an array of user ids in my application. What I want is for every user contained in the ids array to update/add my user id to that users Followers array. Is it possible to do so in one query?

Code for reference. It does not match/update anything. If I pass an empty filter my user id is added to my user Followers array:

var followersUsersAttributes = {
                    "$addToSet": {
                        "Followers": app.user.Id // my user id
                    }
                };

                var followersUsersFilter = new Everlive.Query();
                followersUsersFilter.where()
                    .isin('Id', addToFollowedUsers);

                data.rawUpdate(followersUsersAttributes, followersUsersFilter, function (result) {
                    console.log(JSON.stringify(result));
                }, function (err) {
                    console.log(JSON.stringify(err));
                });

1 Answer, 1 is accepted

Sort by
0
Martin
Telerik team
answered on 19 May 2016, 10:24 AM

Hi Nikola,

The "rawUpdate" filter works just like the regular filter when making a query request - the "rawUpdate" method will update only the records matching the condition in the filter.

Yes, you can achieve what you have described and actually your code looks just right to do the task. I have reproduced the described scenario and it is working just as expected - the "Id" is added to the "Followers" array field only to the filtered users.

Here is how my code looks like:

var el = new Everlive('your-app-id');
var arrayWithIds = ["e0cedb70-1cff-11e6-92e0-27a213a7e82d", "a267f920-1cff-11e6-b247-df34e0fe9a8b"];
var myId = "11af9420-1d99-11e6-a678-29b26c147d2e";
 
var attributes = {
    '$addToSet': {
        'Followers': myId
    }
};
 
// create a filter using Everlive.Query method
var followersUsersFilter = new Everlive.Query();
followersUsersFilter.where()
    .isin('Id', arrayWithIds);
 
// create a filter as an object
// var followersUsersFilter = {
//     'Id': {
//         '$in': arrayWithIds
//     }
// };
 
var data = el.data('Users');
data.rawUpdate(attributes, followersUsersFilter, function(result) {
    console.log(JSON.stringify(result));
}, function(err) {
    console.log(JSON.stringify(err));
});

Have in mind that "$addToSet" operator will only add the value if it is not already present in the array.

You can check what records will be updated with the filter by making a get request:

var el = new Everlive('your-app-id');
var data = el.data('Users');
var arrayWithIds = ["e0cedb70-1cff-11e6-92e0-27a213a7e82d", "a267f920-1cff-11e6-b247-df34e0fe9a8b"];
var query = new Everlive.Query();
query.where().isin('Id', arrayWithIds);
data.get(query) // filter
    .then(function(data){
        alert(JSON.stringify(data));
    },
    function(error){
        alert(JSON.stringify(error));
    });


Do you get some kind of error from the "rawUpdate" response or a success response? If you get response:
{"result":0}
then nothing matches the filter and no rows are updated.

Looking forward for your answer.


Regards,
Martin
Telerik
 
Everlive is now Telerik Backend Services, and is part of the Telerik Platform.
 
Tags
JavaScript SDK
Asked by
Nikola
Top achievements
Rank 1
Answers by
Martin
Telerik team
Share this question
or