Updating multiple rows with rawUpdate

2 posts, 0 answers
  1. Nikola
    Nikola avatar
    1 posts
    Member since:
    May 2016

    Posted 16 May Link to this post

    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));
                    });

  2. Martin
    Admin
    Martin avatar
    67 posts

    Posted 19 May Link to this post

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