Introduction to MongoDB
1.0 - draft
1.0 - draft
  • MongoDB
  • Getting Started
    • General
      • Database? Collection? Schema?
      • RDBMS? NoSQL?
      • Scale-Up ? Scale - out?
      • JavaScript Object Notation
    • MongoDB
      • 간략한 역사,
    • Manipulating Database
      • MongoShell
      • PyMongo
  • CRUD
    • General
      • Explain()
      • mongoimport
    • Create
      • Mongo Shell
      • PyMongo
      • Exercises
    • Read
      • Mongo Shell
      • PyMongo
      • Exercises
    • Update
      • Mongo Shell
      • PyMongo
      • Exercises
    • Delete
      • PyMongo
      • Exercises
    • Challenging Exercises
      • Exercise 1
  • Index with Geospatial & Text
    • Basic Index
      • Syntax
      • Exercises
    • Geospatial Index
      • Syntax
      • Exercises
    • Text Index
      • Syntax
      • Exercises
    • Challenge
  • Aggregate framework
    • Aggregate Framework
      • Basic
      • Exercises
  • Distributed
    • Replication
      • Setup
        • Configuration File
        • Running Servers
        • Initiation
        • Exercises
      • Concerns
        • Read Concern
        • Write Concern
        • Exercises
    • Sharding
      • Getting Started
      • Setup
  • MongoDB 4.0 Features
    • Multi-Document Transaction
      • Transaction?
      • Basic
      • Exercises
  • Administration
    • Untitled
Powered by GitBook
On this page
  • Update Operators
  • Operators

Was this helpful?

  1. CRUD
  2. Update

PyMongo

Update Operators

  • The syntax is identical to update documents between Mongo shell and Pymongo

    • Note that Pymongo uses a dictionary as a document in MongoDB.

    • Warn: Update operators must be written with ' or " , e.g., '$pull‘

 collection.update_one( { 'last_name': ‘Kim' }, { '$set': { 'money' : 1000000 } } )
  • All operators of MongoDB are available in PyMongo

  • $set, $unset, $inc, $push, $each, $addToSet, $pop, $pull, $slice, etc...

  • update_one: Updates a single document within the collection based on the query.

  • update_many: Updates multiple documents within the collection based on the query.

collection.update_one(<query>, <update>[, <options>])
collection.update_many(<query>, <update>[, <options>])

collection.update(<query>, <update>[, <options>]) # Deprecated

Operators

  • Two ways to manipulate values in arrays.

    • By position

    • Position operator (the $ character)

      • When we don’t know what index of the array to modify.

      • Updates only the first match.

    • Example

collection.update_one({<query>}, {"$inc" : {“comments.0.like” : 1}})
collection.update_many({<query>}, {"$inc" : {“comments.$.like” : 1}})
  • Upsert

    • If upsert is True and no documents match the filter, perform an insert.

collection.update_one(< filter >, < update >, upsert=True)
collection.update_many(< filter >, < update >, upsert=True)
PreviousMongo ShellNextExercises

Last updated 5 years ago

Was this helpful?