PyMongo

Manipulate Database/Collection

  • Database List

client.list_database_names()
  • Use a Database

db = client.test_database
# or
db = client['test_database']
  • Collection list

db.list_collection_names()
  • Use a Collection

collection = db['test_collection']
# or
collection = db.test_collection
  • Create a collection

db.create_collection(‘test_collection’)
  • Drop a collection

db.test_collection.drop()
# or
db.drop_collection('test_collection’)
  • Drop a database

client.drop_database(‘test_database’)

Dictionary

  • Using dictionaries to represent documents

import datetime
post = {
"author": "Mike",
"text": "My first blog post!",
"tags": [“MongoDB", “Python", “PyMongo"],
"date": datetime.datetime.utcnow()
}

Last updated