반응형
1. Update
① Update 옵션
- upsert (기본값 : false) : 값이 true로 설정되면 query한 document가 없을 경우, 새로운 document를 추가합니다.
- multi (기본값 : false) : 값이 true로 설정되면 여러개의 document를 수정한다.
② Update 인수
- update({찾을값},{변경값},{옵션});
MongoDB Enterprise > db.users.update({"password":"1234"},{$set:{"password":"5678"}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
MongoDB Enterprise > db.users.find().pretty();
{ "_id" : ObjectId("614c219b60d3cb6c40faa181"), "username" : "love" }
{ "_id" : ObjectId("614c21d960d3cb6c40faa182"), "password" : "5678" }
{
"_id" : ObjectId("614c21e960d3cb6c40faa183"),
"username" : "love",
"password" : "5678",
"age" : 30
}
{
"_id" : ObjectId("614c2b5260d3cb6c40faa184"),
"username" : "bab",
"password" : "1234",
"age" : 35,
"friend" : {
"username" : "ssar",
"password" : "1234",
"age" : 20
}
}
→ 변경 값에 $set을 주지 않으면 전체가 변경된다. 해당 부분을 변경을 위해선 $set을 준다.
→ 특정 필드 삭제하려면 $unset
③ Update 하기
- 일반적인 업데이트는 다른 필드를 다 사라지게 만들어버린다.
MongoDB Enterprise > db.users.update({"username":"ssar"},{"username":"love"});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
MongoDB Enterprise > db.users.find().pretty();
{ "_id" : ObjectId("614c219b60d3cb6c40faa181"), "username" : "love" }
MongoDB Enterprise > db.users.update({"password":"1234"},{$set:{"password":"5678"}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
MongoDB Enterprise > db.users.find().pretty();
{ "_id" : ObjectId("614c21d960d3cb6c40faa182"), "password" : "5678" }
④ 특정필드만 Update 하기
- 옵션에 multi를 주지않으면 찾은 첫 값만 병경한다. 찾은 모든 값을 변경하려면 multi 옵션을 준다.
MongoDB Enterprise > db.users.update({"password":"1234"},{$set:{"password":"5678"}},{multi:true});
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
MongoDB Enterprise > db.users.find().pretty();
{
"_id" : ObjectId("614c21e960d3cb6c40faa183"),
"username" : "love",
"password" : "5678",
"age" : 30
}
{
"_id" : ObjectId("614c2b5260d3cb6c40faa184"),
"username" : "bab",
"password" : "5678",
"age" : 35,
"friend" : {
"username" : "ssar",
"password" : "1234",
"age" : 20
}
}
⑤ Update시 찾은 값이 없으면 Document 추가하기
- upsert 옵션이 찾은 값이 없으면 document를 추가해준다.
MongoDB Enterprise > db.users.update({id:5},{id:5,username:"dica"},{upsert:true});
WriteResult({
"nMatched" : 0,
"nUpserted" : 1,
"nModified" : 0,
"_id" : ObjectId("614d70c57bcf5dfc953c6e31")
})
MongoDB Enterprise > db.users.find().pretty();
{ "_id" : ObjectId("614c21d960d3cb6c40faa182"), "password" : "5678" }
{
"_id" : ObjectId("614c21e960d3cb6c40faa183"),
"username" : "love",
"password" : "5678",
"age" : 30
}
{
"_id" : ObjectId("614c2b5260d3cb6c40faa184"),
"username" : "bab",
"password" : "5678",
"age" : 35,
"friend" : {
"username" : "ssar",
"password" : "1234",
"age" : 20
}
}
{
"_id" : ObjectId("614d70c57bcf5dfc953c6e31"),
"id" : 5,
"username" : "dica"
}
⑥ 특정 필드만 Update문으로 삭제하기 : $unset 명령어를 주면 특정 필드만 삭제할 수 있다.
MongoDB Enterprise > db.users.update({id:5}, {$unset:{username:"dica"}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
MongoDB Enterprise > db.users.find().pretty();
{ "_id" : ObjectId("614c21d960d3cb6c40faa182"), "password" : "5678" }
{
"_id" : ObjectId("614c21e960d3cb6c40faa183"),
"username" : "love",
"password" : "5678",
"age" : 30
}
{
"_id" : ObjectId("614c2b5260d3cb6c40faa184"),
"username" : "bab",
"password" : "5678",
"age" : 35,
"friend" : {
"username" : "ssar",
"password" : "1234",
"age" : 20
}
}
{ "_id" : ObjectId("614d70c57bcf5dfc953c6e31"), "id" : 5 }
2. Remove
① 부분 데이터 삭제
MongoDB Enterprise > db.users.remove({id:5});
WriteResult({ "nRemoved" : 1 })
MongoDB Enterprise > db.users.find().pretty();
{ "_id" : ObjectId("614c21d960d3cb6c40faa182"), "password" : "5678" }
{
"_id" : ObjectId("614c21e960d3cb6c40faa183"),
"username" : "love",
"password" : "5678",
"age" : 30
}
{
"_id" : ObjectId("614c2b5260d3cb6c40faa184"),
"username" : "bab",
"password" : "5678",
"age" : 35,
"friend" : {
"username" : "ssar",
"password" : "1234",
"age" : 20
}
}
② 전체 데이터 삭제 : 되돌리기 불가!
MongoDB Enterprise > db.users.remove({});
WriteResult({ "nRemoved" : 3 })
MongoDB Enterprise > db.users.find().pretty();
MongoDB Enterprise >
③ 컬렉션 삭제 : 되돌리기 불가!
MongoDB Enterprise > db.users.drop();
true
반응형
'DataBase > Mongo DB' 카테고리의 다른 글
Mongo DB 7강 - 주기마다 데이터 수집 및 DB 저장 (0) | 2021.10.01 |
---|---|
Mongo DB 6강 - 배열 연산자 (고급) (0) | 2021.09.27 |
Mongo DB 4강 - Save, Find, 연산 (0) | 2021.09.23 |
Mongo DB 3강 - Spring Boot 연결 / CRUD (0) | 2021.09.23 |
Mongo DB 2강 - RDBS vs NoSQL / 접속 (0) | 2021.09.23 |