比较,添加,更新,删除mongodb数组上的元素



需要有关以下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"
    }
}

谢谢

萨蒂什·拉卡尼

相关内容

  • 没有找到相关文章

最新更新