使用mongo集合对象中的元素键更改对象数组中对象的顺序

问题描述 投票:1回答:1

我想改变幻灯片子文档中对象的sort_order键的顺序。

对于Eg:我想移动标题A3高于A2的幻灯片。即。将A3(3)的sort_order值更改为2,将A2的值更改为3,反之则将其更改为3。

怎么做 ??

下面是db对象。

{
    "_id" : ObjectId("5a3b8cc68884dd1140a9a5b8"),
    "title" : "Sort Test",
    "user_id" : ObjectId("59e08e45f081170f582e95cc"),
    "status" : "active",
    "slides" : [ 
        {
            "_id" : ObjectId("5a3b8cc68884dd1140a9a5b9"),
            "sort_order" : 0,
            "content" : "Sort Test",
            "title" : "Sort Test"
        }, 
        {
            "_id" : ObjectId("5a3b8cda8884dd1140a9a5ba"),
            "sort_order" : 1,
            "content" : "Text Contents here...",
            "title" : "A1"
        }, 
        {
            "_id" : ObjectId("5a3b8ce48884dd1140a9a5bb"),
            "sort_order" : 2,
            "content" : "Text Contents here...",
            "title" : "A2"
        }, 
        {
            "_id" : ObjectId("5a3b8cec8884dd1140a9a5bc"),
            "sort_order" : 3,
            "content" : "Text Contents here...",
            "title" : "A3"
        },
        {
            "_id" : ObjectId("5a3b8cec8884dd1140455bc"),
            "sort_order" : 4,
            "content" : "Text Contents here...",
            "title" : "A4"
        }
    ],
    "description" : "Sort Test",
    "__v" : 0
}
javascript arrays node.js mongodb mongoose
1个回答
1
投票

使用这种方法,您可以创建一个具有相同元素的新数组,以及您要求的元素,切换(我认为就是您所问的)。然后,您只需使用新数组更新数据库中的幻灯片字段。

var dbObject = {
    "_id" : ObjectId("5a3b8cc68884dd1140a9a5b8"),
    "title" : "Sort Test",
    "user_id" : ObjectId("59e08e45f081170f582e95cc"),
    "status" : "active",
    "slides" : [ 
        {
            "_id" : ObjectId("5a3b8cc68884dd1140a9a5b9"),
            "sort_order" : 0,
            "content" : "Sort Test",
            "title" : "Sort Test"
        }, 
        {
            "_id" : ObjectId("5a3b8cda8884dd1140a9a5ba"),
            "sort_order" : 1,
            "content" : "Text Contents here...",
            "title" : "A1"
        }, 
        {
            "_id" : ObjectId("5a3b8ce48884dd1140a9a5bb"),
            "sort_order" : 2,
            "content" : "Text Contents here...",
            "title" : "A2"
        }, 
        {
            "_id" : ObjectId("5a3b8cec8884dd1140a9a5bc"),
            "sort_order" : 3,
            "content" : "Text Contents here...",
            "title" : "A3"
        },
        {
            "_id" : ObjectId("5a3b8cec8884dd1140455bc"),
            "sort_order" : 4,
            "content" : "Text Contents here...",
            "title" : "A4"
        }
    ],
    "description" : "Sort Test",
    "__v" : 0
}

var slides = dbObject.slides;

var switchElementsInArray = function(array, sortOrder1, sortOrder2) {
    var newArray = [];
    for(var element of array) {
        if(element.sort_order === sortOrder1) {
            newArray.push(elem2);
        } else if(element.sort_order === sortOrder2) {
            newArray.push(elem1);
        } else {
            newArray.push(element);
        }
    }

    return newArray;
}


var newSlides = switchElementsInArray(slides, 2, 3);
© www.soinside.com 2019 - 2024. All rights reserved.