The most efficient way to change the value of a string field in a substring

I have a collection filled with documents that look like this:

{
    data: 11,
    version: "0.0.32"  
}

and some have a suffix testbefore version:

{
    data: 55,
    version: "0.0.42-test"  
}

The field versionhas a different value, but it always follows the pattern: 0.0.XXX. I would like to update all the documents so that they look like this:

{
    data: 11,
    version: 32  
}

and the suffix version (for test documents - versionmust be negative):

{
    data: 55,
    version: -42  
}

The collection with these documents is used by our critical system, which must be disabled when updating data, so I want the update / change to be as quick as possible. There are about 66_000_000documents in this collection , about 100 GB in size.

What type of mongodb operation would be most effective?

+4
1

- MongoDB $split , , $let $arrayElemAt.

$switch case .

$gt, true, "test", in $concat -. false, .

, $indexOfCP, -1, "test".

let cursor = db.collection.aggregate(
    [
        { "$project": { 
            "data": 1, 
            "version": { 
                "$let": { 
                    "vars": { 
                        "v": { 
                            "$arrayElemAt": [
                                { "$split": [ "$version", "." ] }, 
                                -1
                            ]
                        }
                    }, 
                    "in": { 
                        "$switch": { 
                            "branches": [ 
                                { 
                                    "case": { 
                                        "$gt": [ 
                                            { "$indexOfCP": [ "$$v", "test" ] },
                                            -1 
                                        ]
                                    }, 
                                    "then": { 
                                        "$concat": [ 
                                            "-", 
                                            "", 
                                            { "$arrayElemAt": [
                                                { "$split": [ "$$v", "-" ] }, 
                                                0 
                                            ]} 
                                        ]
                                    }
                                }
                            ], 
                            "default": "$$v" 
                        }
                    }
                }
            }
        }}
    ]
)

:

{ "_id" : ObjectId("57a98773cbbd42a2156260d8"), "data" : 11, "version" : "32" }
{ "_id" : ObjectId("57a98773cbbd42a2156260d9"), "data" : 55, "version" : "-42" }

, "" . , $out .

{ "out": "collection" }

, , MongoDB , , - Cursor parseFloat Number, $set bulkWrite() .

let requests = [];
cursor.forEach(doc => { 
    requests.push({ 
        "updateOne": { 
            "filter": { "_id": doc._id }, 
            "update": { 
                "$set": { 
                    "data": doc.data, 
                    "version": parseFloat(doc.version) 
                },
                "$unset": { "person": " " }
            } 
        } 
    }); 
    if ( requests.length === 1000 ) { 
        // Execute per 1000 ops and re-init
        db.collection.bulkWrite(requests); 
        requests = []; 
    }} 
);

 // Clean up queues
if(requests.length > 0) {
    db.coll.bulkWrite(requests);
}

MongoDB 3.4 , MongoDB 3.2 mapReduce bulkWrite().

var results = db.collection.mapReduce(
    function() { 
        var v = this.version.split(".")[2]; 
        emit(this._id, v.indexOf("-") > -1 ? "-"+v.replace(/\D+/g, '') : v)
    }, 
    function(key, value) {}, 
    { "out": { "inline": 1 } }
)["results"];

results :

[
    {
        "_id" : ObjectId("57a98773cbbd42a2156260d8"),
        "value" : "32"
    },
    {
        "_id" : ObjectId("57a98773cbbd42a2156260d9"),
        "value" : "-42"
    }
]

.forEach .


MongoDB 2.6 3.0 Bulk() API , .

+4

Source: https://habr.com/ru/post/1650566/


All Articles