Eén document bijwerken

Introductie tot MongoDB in Python

Filip Schouwenaars

Machine Learning Researcher

Waarom updaten of vervangen?

  • Na het toevoegen evolueert data
    • Fouten corrigeren
    • Missende info toevoegen
    • Hele documenten herstructureren
  • MongoDB biedt tools om te updaten en te vervangen
Introductie tot MongoDB in Python

Eén document bijwerken

# Define filter to find document to change
query_filter = { "title": "la la land" }

# Describe the change update = { "$set": { "release_year": 2016 } }
# Perform update res = mov.update_one(query_filter, update)
res.modified_count
1
Introductie tot MongoDB in Python

Meerdere documenten bijwerken

# Define filter (can match multiple documents!)
query_filter = { "genre": "comedy" }

# Describe the change update = { "$set": { "is_funny": True } }
# Apply change to all matching documents result = mov.update_many(query_filter, update)
print(result.modified_count)
9
Introductie tot MongoDB in Python

Een heel document vervangen

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,
  ...
}
Introductie tot MongoDB in Python

Laten we oefenen!

Introductie tot MongoDB in Python

Preparing Video For Download...