【问题标题】:Changing order of object in object array with an element key in mongo collection object使用 mongo 集合对象中的元素键更改对象数组中对象的顺序
【发布时间】:2018-06-04 02:39:35
【问题描述】:

我想根据其中的 sort_order 键更改幻灯片子文档中对象的顺序。

例如:我想将标题为 A3 的幻灯片移到 A2 上方。 IE。 sort_orderA3 (3)的值改为2A2的值将改为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】:

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

    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);
    

    【讨论】:

      猜你喜欢
      • 2017-07-29
      • 1970-01-01
      • 2018-10-29
      • 1970-01-01
      • 1970-01-01
      • 2021-12-23
      • 2021-02-25
      • 1970-01-01
      • 2021-06-26
      相关资源
      最近更新 更多