Removing white spaces (leading and trailing) from string value

后端 未结 4 463
情歌与酒
情歌与酒 2020-12-10 00:00

I have imported a csv file in mongo using mongoimport and I want to remove leading and trailing white spaces from my string value.

Is it possible directly in mongo t

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-10 00:17

    Small correction to the answer from Neil for bulk operations api

    it is

    initializeOrderedBulkOp
    

    not

    initializeBulkOrderedOp
    

    also you missed to

    counter++;
    

    inside the forEach, so in summary

    var counter = 1;
    var bulk = db.collection.initializeOrderedBulkOp();
    db.collection.find({ "category": /^\s+|\s+$/ },{ "category": 1}).forEach(
        function(doc) {
            bulk.find({ "_id": doc._id }).update({
                "$set": { "category": doc.category.trim() }
            });
    
            if ( counter % 1000 == 0 ) {
                bulk.execute();
                counter = 1;
            }
            counter++;
        }
    );
    
    if ( counter > 1 )
        bulk.execute();
    

    Note: I don't have enough reputation to comment, hence adding an answer

提交回复
热议问题