DB
[Mongodb] Set, Push 사용법
젠워드
2023. 4. 17. 09:20
import pymongo
client = pymongo.MongoClient("<mongodb_uri>")
db = client.test
# 업데이트할 컬렉션
collection = db.test_collection
# 업데이트할 문서 쿼리
query = {"name": "Alice"}
# $set 연산자를 사용하여 필드 값을 업데이트
new_field = {"$set": {"age": 30}}
collection.update_one(query, new_field)
# $push 연산자를 사용하여 배열 필드에 값을 추가
new_field = {"$push": {"hobbies": "reading"}}
collection.update_one(query, new_field)
반응형