代码之家  ›  专栏  ›  技术社区  ›  user1063287

如何从数组中删除对象并更新所有其他对象的位置值?

  •  1
  • user1063287  · 技术社区  · 7 年前

    本机节点驱动程序
    MongoDB 4.0.2版

    期望的行为

    根据对象的属性从对象数组中删除对象 id ,并减少其他对象的 position 价值(如适用)。

    具体来说,我想:

    迭代MongoDB中的对象数组并更新中的属性 每个对象

    也许它可能涉及使用 $inc -1 ,但我仍然不确定如何进行迭代。

    我试过的

    我的逻辑在为一个 JavaScript 场景(在浏览器控制台中工作):

    function remove_and_update_position(id) {
    
        // faux array in collection document
        var statements = [{
                position: 0,  // < i want to update 'position' of all objects after an object is removed  
                id: "a"
            },
            {
                position: 1,
                id: "b"
            },
            {
                position: 2,
                id: "c"
            },
            {
                position: 3,
                id: "d"
            },
            {
                position: 4,
                id: "e"
            }
    
        ];
    
        // delete an object from the 'statements' array by its id
        statements.splice(statements.indexOf(statements.find(item => item.id == id)), 1);
    
        // iterate over the modified array
        for (var i = 0; i < statements.length; i++) {
    
            // for all objects in the modified array,  
            // with position numbers greater than the  
            // position removed ( minus 1),  
            // decrement their position numbers
            if (i > position - 1) {
                var new_position = statements[i].position - 1;
                statements[i].position = new_position;
            }
    
        }
    
        // verify adjustments were correct
        console.log(statements);
    
    }
    
    // call the function
    remove_and_update_position("c");
    

    MongoDB/Node 场景-到目前为止,我只有对象删除部分:

    const remove_and_update_position = (req, res) => {
    
        var request_body = req.body;
    
        var topic_id = request_body.topic_id;
    
        var o_id = new ObjectID(topic_id);
    
        var collection = mongo_client.db("topics").collection("topics");
    
        var statement_id = request_body.statement_id; 
    
        var position = Number(request_body.position); 
    
        // query for the relevant document *and* target nested statement by id
        var filter = { _id: o_id, "statements.id": statement_id };
    
        // remove from statements array
        var update = { $pull: { "statements": { id: statement_id } } };
    
        // perform the update
        collection.updateOne(filter, update, function(err, doc) {
            if (err) {
                res.send(err);
            } else {
                res.json({ response_type: "success" });
            }
        });
    
    }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   user1063287    7 年前

    根据更新 answer here

    const remove_and_update_position = (req, res) => {
    
        var request_body = req.body;
    
        var topic_id = request_body.topic_id;
    
        var o_id = new ObjectID(topic_id);
    
        var collection = mongo_client.db("topics").collection("topics");
    
        var statement_id = request_body.statement_id;
    
        var position = Number(request_body.position);
    
        // query for the relevant document *and* target nested statement by id
        var filter = { _id: o_id, "statements.id": statement_id };
    
        // remove from statements array
        var update = { $pull: { "statements": { id: statement_id } } };
    
        // perform the update
        collection.updateOne(filter, update, function(err, doc) {
            if (err) {
                res.send(err);
            } else {
    
                // BEGIN decrement others objects position
                var position_indicator = position - 1;
    
                console.log("deleted object position: " + position);
                console.log("will decrement objects with position greater than: " + position_indicator);
    
                var new_filter = { _id: o_id, "statements.position": { $gt: position_indicator } };
    
                var new_update = { $inc: { "statements.$[elem].position": -1 } };
    
                var array_filters = { "arrayFilters": [{ "elem.position": { $gt: position_indicator } }], "multi": true }; 
    
                collection.updateOne(new_filter, new_update, array_filters, function(err, doc) {
                    if (err) {
                        res.send(err);
                    } else {
                        res.json({ response_type: "success" });
                    }
                });
                // END decrement others objects position
            }
        });
    
    }
    
    推荐文章