Javascript 比较、添加、更新、删除mongodb数组中的元素

Javascript 比较、添加、更新、删除mongodb数组中的元素,javascript,arrays,mongodb,compare,insert-update,Javascript,Arrays,Mongodb,Compare,Insert Update,在MongoDB的以下文档中需要更新、删除、添加、升级、删除等操作的帮助。 下面是temp集合中存在的MongoDB文档 { "local_id" : "1841", "name_first" : "tiger", "name_last" : "lion", "address" : [ { "id" : 1,

MongoDB
的以下文档中需要更新、删除、添加、升级、删除等操作的帮助。 下面是
temp
集合中存在的
MongoDB
文档

{       

            "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"
            }
        ] 

     }`
现在我有一些文件如下,我想查询,将比较,添加,更新,删除我的上述文件

`
    {       

           "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*/
            ] 

        }`

您可以在服务器上保存函数,因为您可以调用该函数来获取差异,如下所示

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);
    }
})
然后您可以按如下方式调用函数

db.loadServerScripts();
getupdatedArray({"a": "abc"}, {"a": "a111", "b": "bbb"});
这将为您提供以下结果:

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


Satish Lakhani

为什么要对其进行比较、添加、更新和删除。如果你想优先考虑第二个文档,只需替换完整的文档。我还想维护日志详细信息,以便进行必要的比较。