단일 문서 업데이트

Python으로 배우는 MongoDB 입문

Filip Schouwenaars

Machine Learning Researcher

왜 업데이트/교체하나요?

  • 추가 후에도 데이터는 변합니다
    • 오류 수정
    • 누락 정보 추가
    • 문서 구조 재정비
  • MongoDB는 업데이트와 교체 도구를 제공합니다
Python으로 배우는 MongoDB 입문

단일 문서 업데이트

# 변경할 문서를 찾는 필터 정의
query_filter = { "title": "la la land" }

# 변경 내용 지정 update = { "$set": { "release_year": 2016 } }
# 업데이트 실행 res = mov.update_one(query_filter, update)
res.modified_count
1
Python으로 배우는 MongoDB 입문

여러 문서 업데이트

# 필터 정의(여러 문서와 일치할 수 있음)
query_filter = { "genre": "comedy" }

# 변경 내용 지정 update = { "$set": { "is_funny": True } }
# 일치하는 모든 문서에 적용 result = mov.update_many(query_filter, update)
print(result.modified_count)
9
Python으로 배우는 MongoDB 입문

전체 문서 교체

query_filter = { "title": "the lion king" }

mov.find_one(query_filter)
{
   '_id': '68375bf5a63f71e478cdc7f5'
   'title': 'the lion king', 
   'release_year': 1994,
   ...
}
replacement = {
  "title": "the lion king",
  "genre": ["animation", "adventure", "drama"],
  "release_year": 2019,
  "rating": 6.8
}

mov.replace_one(query_filter, replacement)
mov.find_one(query_filter)
{
  '_id': '68375bf5a63f71e478cdc7f5',
  'title': 'the lion king'
  'release_year': 2019,
  ...
}
Python으로 배우는 MongoDB 입문

연습해 봅시다!

Python으로 배우는 MongoDB 입문

Preparing Video For Download...