MongoDB에는 집계 프레임워크의 일부로 사용할 $toLower 연산자가 있습니다. 그러나 for 루프를 사용하여 특정 필드를 반복하고 하나씩 업데이트할 수도 있습니다.
먼저 문서로 컬렉션을 생성하겠습니다.
> 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")
} 다음은 find() 메서드를 사용하여 컬렉션의 모든 문서를 표시하는 쿼리입니다.
> db.toLowerDemo.find().pretty();
그러면 다음과 같은 출력이 생성됩니다.
{
"_id" : ObjectId("5c9b1b4515e86fd1496b38bf"),
"StudentId" : 101,
"StudentName" : "John"
}
{
"_id" : ObjectId("5c9b1b4b15e86fd1496b38c0"),
"StudentId" : 102,
"StudentName" : "Larry"
}
{
"_id" : ObjectId("5c9b1b5115e86fd1496b38c1"),
"StudentId" : 103,
"StudentName" : "CHris"
}
{
"_id" : ObjectId("5c9b1b5a15e86fd1496b38c2"),
"StudentId" : 104,
"StudentName" : "ROBERT"
} 다음은 $toLower
와 같은 MongoDB를 업데이트하는 쿼리입니다.> db.toLowerDemo.find().forEach(
... function(lower) {
... lower.StudentName = lower.StudentName.toLowerCase();
... db.toLowerDemo.save(lower);
... }
... ); 위 모음집에서 문서를 다시 한 번 확인해보자. 다음은 쿼리입니다.
> db.toLowerDemo.find().pretty();
그러면 다음과 같은 출력이 생성됩니다.
{
"_id" : ObjectId("5c9b1b4515e86fd1496b38bf"),
"StudentId" : 101,
"StudentName" : "john"
}
{
"_id" : ObjectId("5c9b1b4b15e86fd1496b38c0"),
"StudentId" : 102,
"StudentName" : "larry"
}
{
"_id" : ObjectId("5c9b1b5115e86fd1496b38c1"),
"StudentId" : 103,
"StudentName" : "chris"
}
{
"_id" : ObjectId("5c9b1b5a15e86fd1496b38c2"),
"StudentId" : 104,
"StudentName" : "robert"
}