Search code examples
javascriptarraysmongodbcompareinsert-update

Compare,add,update,delete elements on array of mongodb


Need help on operation like update,delete,add,upsert,delete on below document of MongoDB. Below is MongoDB document that exists in temp collection.

{       

            "local_id" : "1841",

            "name_first" : "tiger", 

            "name_last" : "lion",

            "address" : [
              {
                "id" : 1,
                "address_type" : "Home", 
                "city" : "Delhi", 
                "country" : "", 
                "po_box" : ""
            }, 
            {
                "id" : 2, 
                "address_type" : "Work", 
                "city" : "", 
                "country" : "", 
                "po_box" : ""
            }
        ],

        "email" : [
            {
                "email_id" : "blah@gmail.com", 
                "id" : 1, 
                "type" : "Home"
            }, 
            {
                "email_id" : "Pearl1@gmail.com", 
                "id" : 2, 
                "type" : "Work"
            }
        ], 

 "phone_number" : [
            {
                "id" : 1, 
                "no" : "+911234567890", 
                "type" : "Mobile"
            },
            {
                "id" : 2, 
                "no" : "+917894561230", 
                "type" : "work"
            }
        ] 

     }`

Now I have some document like below, i want query that will compare,add,update,delete on my above document.

`
    {       

           "local_id" : "1730",
           "name_first" : "lion", 
           "name_last" : "king", 

           "address" : [
                {
                    "id" : 1,
                    "address_type" : "Home", 
                    "city" : "Delhi", 
                    "country" : "India", 
                    "po_box" : "110041"
                }, 
                {
                    "id" : 2, 
                    "address_type" : "Work", 
                    "city" : "Delhi-NCR", 
                    "country" : "India", 
                    "po_box" : "110048"
                },
                {
                    "id" : 3, 
                    "address_type" : "Work", 
                    "city" : "Delhi-NCR", 
                    "country" : "Indai", 
                    "po_box" : "110048"
                }
            ],

            "email" : [
                {
                    "email_id" : "updatethis@gmail.com", 
                    "id" : 1, 
                    "type" : "Home"
                }, 
                {
                    "email_id" : "Pearl1@gmail.com", 
                    "id" : 2, 
                    "type" : "Work"
                },
                {
                    "email_id" : "addthisarray@gmail.com", 
                    "id" : 3, 
                    "type" : "personal"
                }
            ], 

            "phone_number" : [
                {
                    "id" : 1, 
                    "no" : "+911234567890", 
                    "type" : "Mobile"
                }
                /*second array not here so remove that array from that document*/
            ] 

        }`

Solution

  • You can save function on server as you can call that function to get the differences, as below.

    db.system.js.save({
        _id: "getupdatedArray",
        value: function(obj1, obj2) {
            var VALUE_CREATED = 'created';
            var VALUE_UPDATED = 'updated';
            var VALUE_DELETED = 'deleted';
            var VALUE_UNCHANGED = 'unchanged';
    
            function map(obj1, obj2) {
                if (isValue(obj1) || isValue(obj2)) {
                    return {
                        type: compareValues(obj1, obj2),
                        old: obj1,
                        new: obj2
                    };
                }
    
                var diff = {};
                for (var key in obj1) {
                    if (isFunction(obj1[key])) {
                        continue;
                    }
    
                    var value2 = undefined;
                    if ('undefined' != typeof(obj2[key])) {
                        value2 = obj2[key];
                    }
    
                    diff[key] = map(obj1[key], value2);
                }
                for (var key in obj2) {
                    if (isFunction(obj2[key]) || ('undefined' != typeof(diff[key]))) {
                        continue;
                    }
    
                    diff[key] = map(undefined, obj2[key]);
                }
    
                return diff;
            }
    
            function compareValues(value1, value2) {
                if (value1 === value2) {
                    return VALUE_UNCHANGED;
                }
                if ('undefined' == typeof(value1)) {
                    return VALUE_CREATED;
                }
                if ('undefined' == typeof(value2)) {
                    return VALUE_DELETED;
                }
    
                return VALUE_UPDATED;
            }
    
            function isFunction(obj) {
                return {}.toString.apply(obj) === '[object Function]';
            }
    
            function isArray(obj) {
                return {}.toString.apply(obj) === '[object Array]';
            }
    
            function isObject(obj) {
                return {}.toString.apply(obj) === '[object Object]';
            }
    
            function isValue(obj) {
                return !isObject(obj) && !isArray(obj);
            }
    
            return map(obj1, obj2);
        }
    })
    

    Then you can call function as below..

    db.loadServerScripts();
    getupdatedArray({"a": "abc"}, {"a": "a111", "b": "bbb"});
    

    This will give you result as below:

    { 
        "a" : {
            "type" : "updated", 
            "old" : "abc", 
            "new" : "a111"
        }, 
        "b" : {
            "type" : "created", 
            "old" : undefined, 
            "new" : "bbb"
        }
    }
    

    Thanks

    Satish Lakhani