$unset 연산자를 사용하여 속성을 설정 해제합니다. 먼저 문서로 컬렉션을 만들어 보겠습니다. −
> db.unsetAnAttributeDemo.insertOne(
... {
... _id: 1,
... "StudentDetails": [
... {
... "StudentFirstName": "Ramit",
... "StudentCountryName":"UK"
... },
... {
... "StudentFirstName": "Bob",
... "StudentCountryName":"US"
... },
... {
... "StudentFirstName": "Carol",
... "StudentCountryName":"AUS"
...
... }
... ]
... }
... );
{ "acknowledged" : true, "insertedId" : 1 } 다음은 find() 메서드를 사용하여 컬렉션의 모든 문서를 표시하는 쿼리입니다. -
> db.unsetAnAttributeDemo.find().pretty();
이것은 다음과 같은 출력을 생성합니다 -
{
"_id" : 1,
"StudentDetails" : [
{
"StudentFirstName" : "Ramit",
"StudentCountryName" : "UK"
},
{
"StudentFirstName" : "Bob",
"StudentCountryName" : "US"
},
{
"StudentFirstName" : "Carol",
"StudentCountryName" : "AUS"
}
]
} 다음은 단일 배열 요소에서 속성을 설정 해제하는 쿼리입니다. 값이 "AUS"인 "StudentCountryName" 속성이 설정 해제됩니다. −
> db.unsetAnAttributeDemo.update({"StudentDetails.StudentCountryName": "AUS"}, {$unset:
{"StudentDetails.$.StudentCountryName": 1}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) 값이 "AUS"인 StudentCountryName 속성이 지워졌는지 확인하기 위해 컬렉션의 문서를 표시해 보겠습니다. -
> db.unsetAnAttributeDemo.find().pretty();
이것은 다음과 같은 출력을 생성합니다 -
{
"_id" : 1,
"StudentDetails" : [
{
"StudentFirstName" : "Ramit",
"StudentCountryName" : "UK"
},
{
"StudentFirstName" : "Bob",
"StudentCountryName" : "US"
},
{
"StudentFirstName" : "Carol"
}
]
}