Last active
May 16, 2018 02:35
-
-
Save gchavez2/eb1e6de2e4ebbb11aa21d85b4e4db181 to your computer and use it in GitHub Desktop.
PyMongo Hello World
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pymongo import MongoClient | |
import datetime | |
from datetime import timedelta | |
import pprint | |
client = MongoClient('mongodb://localhost:27017/') | |
db = client['demodb'] | |
collection = db['test_collection'] | |
print('Droping collection') | |
db.collection.drop() | |
entry1 = { | |
"author": "GC", | |
"text": "My first blog post!", | |
"tags": ["mongodb", "python", "pymongo"], | |
"date": datetime.datetime.utcnow() - timedelta(days=1) | |
} | |
entry2 = { | |
"author": "GICC", | |
"text": "My second blog post!", | |
"tags": ["mongodb", "python", "pymongo"], | |
"date": datetime.datetime.utcnow() | |
} | |
entry_inserted_id = db.collection.insert_one(entry1).inserted_id | |
print( "Inserted entry: " + str(entry_inserted_id) ) | |
entry_inserted_id = db.collection.insert_one(entry2).inserted_id | |
print( "Inserted entry: " + str(entry_inserted_id) ) | |
for a in db.collection.find(): | |
pprint.pprint(a) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment