MongoDBPython Driver

Syntax

  • mongodb://[username:[email protected]]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]

Parameters

ParameterDetail
hostXOptional. You can specify as many hosts as necessary. You would specify multiple hosts, for example, for connections to replica sets.
:portXOptional. The default value is :27017 if not specified.
databaseOptional. The name of the database to authenticate if the connection string includes authentication credentialsIf /database is not specified and the connection string includes credentials, the driver will authenticate to the admin database.
?optionsConnection specific options

Connect to MongoDB using pymongo

from pymongo import MongoClient

uri = "mongodb://localhost:27017/"

client = MongoClient(uri)

db = client['test_db']
# or
# db = client.test_db

# collection = db['test_collection']
# or
collection = db.test_collection


collection.save({"hello":"world"})

print collection.find_one()

PyMongo queries

Once you got a collection object, queries use the same syntax as in the mongo shell. Some slight differences are:

  • every key must be enclosed in brackets. For example:

    db.find({frequencies: {$exists: true}})
    

    becomes in pymongo (note the True in uppercase):

    db.find({"frequencies": { "$exists": True }})
    
  • objects such as object ids or ISODate are manipulated using python classes. PyMongo uses its own ObjectId class to deal with object ids, while dates use the standard datetime package. For example, if you want to query all events between 2010 and 2011, you can do:

     from datetime import datetime
    
     date_from = datetime(2010, 1, 1)
     date_to = datetime(2011, 1, 1)
     db.find({ "date": { "$gte": date_from, "$lt": date_to } }):
    

Update all documents in a collection using PyMongo

Let's say you need to add a field to every document in a collection.

import pymongo

client = pymongo.MongoClient('localhost', 27017)
db = client.mydb.mycollection

for doc in db.find():
   db.update(
       {'_id': doc['_id']}, 
       {'$set': {'newField': 10} }, upsert=False, multi=False)

The find method returns a Cursor, on which you can easily iterate over using the for in syntax. Then, we call the update method, specifying the _id and that we add a field ($set). The parameters upsert and multi come from mongodb (see here for more info).