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

时间:2017-02-02 04:52:32

标签: javascript arrays mongodb compare insert-update

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

{       

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

        }`

1 个答案:

答案 0 :(得分:4)

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

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