Tutorial by Examples

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"...
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": ...
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...

Page 1 of 1