user6704883
user6704883

Reputation:

Changing strings in mongodb collection to uppercase

I've searched a lot but still can't find (or understand) the answer. I have a collection in my mongodb database that is called "btest". Inside of that collection I have a list of randomly generated strings (from 1000 to 1000000) that look something like this:

> db.btest.find()
{ "_id" : ObjectId("5818ed42c33b12a7c902cd34"), "0" : 1, "wgickjkwxfimleot" : "r
scjuvarvmvuheom" }
{ "_id" : ObjectId("5818ed42c33b12a7c902cd35"), "0" : 2, "wgickjkwxfimleot" : "t
gdqnegjscsmnjsi" }
{ "_id" : ObjectId("5818ed42c33b12a7c902cd36"), "0" : 4, "wgickjkwxfimleot" : "d
qjvndthelmtqknj" }
{ "_id" : ObjectId("5818ed42c33b12a7c902cd37"), "0" : 5, "wgickjkwxfimleot" : "u
qtmbuhgwxntcixh" }
{ "_id" : ObjectId("5818ed42c33b12a7c902cd38"), "0" : 6, "wgickjkwxfimleot" : "i
rguwjvectjvimjk" }
{ "_id" : ObjectId("5818ed42c33b12a7c902cd39"), "0" : 7, "wgickjkwxfimleot" : "n
sggjpodfvebjumk" }
{ "_id" : ObjectId("5818ed42c33b12a7c902cd3a"), "0" : 8, "wgickjkwxfimleot" : "a
wvjtlxtoqwpdltp" }

I imported these using this command:

mongoimport -d ee_db -c btest --type csv --file "C:\Users\USER\Desktop\Random\projekty java\ee_bulk_insert\src\10000.csv" --headerline

What I want to do is to change all the strings in this collection to uppercase letters. In MySQL I've done the same thing with the "SELECT UCASE(row) FROM btest;" command.

How to achieve the same result in MongoDB? Thanks for all the answers.

Upvotes: 1

Views: 1427

Answers (1)

Bertrand Martel
Bertrand Martel

Reputation: 45402

You can use an aggregation with a $project using $toUpper to convert string to uppercase and then write the results in a new collection with $out :

db.btest.aggregate(
    [{
        $project: {
            "0": 1,
            "wgickjkwxfimleot": { $toUpper: "$wgickjkwxfimleot" }
        }
    }, {
        $out: "results"
    }]
)

Upvotes: 1

Related Questions