শেল forEach + toLowerCase() + save()
MongoDB-র অগ리게েশনে $toLower অপারেটর আছে (MongoDB 4.0+), কিন্তু সরাসরি আপডেটে ব্যবহার করা যায় না। শেলে লুপ করে প্রতিটি ডকুমেন্ট আপডেট করা সহজ পদ্ধতি।
উদাহরণ: ডকুমেন্ট তৈরি (মিশ্র কেস)
> db.toLowerDemo.insertOne({"StudentId":101,"StudentName":"John"});
{ "acknowledged" : true, "insertedId" : ObjectId("5c9b1b4515e86fd1496b38bf") }
> db.toLowerDemo.insertOne({"StudentId":102,"StudentName":"Larry"});
{ "acknowledged" : true, "insertedId" : ObjectId("5c9b1b4b15e86fd1496b38c0") }
> db.toLowerDemo.insertOne({"StudentId":103,"StudentName":"CHris"});
{ "acknowledged" : true, "insertedId" : ObjectId("5c9b1b5115e86fd1496b38c1") }
> db.toLowerDemo.insertOne({"StudentId":104,"StudentName":"ROBERT"});
{ "acknowledged" : true, "insertedId" : ObjectId("5c9b1b5a15e86fd1496b38c2") }
আপডেট: সব StudentName লোয়ারকেস
> db.toLowerDemo.find().forEach(function(doc) {
doc.StudentName = doc.StudentName.toLowerCase();
db.toLowerDemo.save(doc);
});
ফাইনাল ডেটা
> db.toLowerDemo.find().pretty();
আউটপুট:
{ "StudentId": 101, "StudentName": "john" }
{ "StudentId": 102, "StudentName": "larry" }
{ "StudentId": 103, "StudentName": "chris" }
{ "StudentId": 104, "StudentName": "robert" }
আধুনিক পদ্ধতি (MongoDB 4.2+): Pipeline Update
db.toLowerDemo.updateMany(
{},
[{ $set: { "StudentName": { $toLower: "$StudentName" } } }]
);
এটি একক আটোমিক অপারেশনে সব ডকুমেন্ট আপডেট করে (দ্রুত এবং সেফ)।