여러 요소를 업데이트하려면 $[]를 사용합니다. $[]는 업데이트 연산자가 지정된 배열 필드의 모든 요소를 수정해야 함을 나타내는 모든 위치 연산자입니다.
먼저 문서로 컬렉션을 만들어 보겠습니다. −
> db.demo385.insertOne({"ServerLogs": [
... {
... "status":"InActive"
... },
... {
... "status":"InActive"
... },
... {
... "status":"InActive"
... }
... ]
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("5e5b6a7522064be7ab44e7f5")
} find() 메서드를 사용하여 컬렉션의 모든 문서 표시 -
> db.demo385.find().pretty();
이것은 다음과 같은 출력을 생성합니다 -
{
"_id" : ObjectId("5e5b6a7522064be7ab44e7f5"),
"ServerLogs" : [
{
"status" : "InActive"
},
{
"status" : "InActive"
},
{
"status" : "InActive"
}
]
} 다음은 MongoDB에서 배열의 여러 요소를 업데이트하는 쿼리입니다 -
> db.demo385.update(
... { "_id" : ObjectId("5e5b6a7522064be7ab44e7f5") },
... { "$set": { "ServerLogs.$[].status": "Active" }}
... )
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) find() 메서드를 사용하여 컬렉션의 모든 문서 표시 -
> db.demo385.find().pretty();
이것은 다음과 같은 출력을 생성합니다 -
{
"_id" : ObjectId("5e5b6a7522064be7ab44e7f5"),
"ServerLogs" : [
{
"status" : "Active"
},
{
"status" : "Active"
},
{
"status" : "Active"
}
]
}