PyMongo Monday Episode 2 : Create

Last time we showed you how to setup up your environment.

In the next few episodes we will take you through the standard CRUD operators that every database is expected to support. In this episode we will focus on the Create in CRUD.


Lets look at how we insert JSON documents into MongoDB.

First lets start a local single instance of mongod using m.

$ m use stable
2018-08-28T14:58:06.674+0100 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2018-08-28T14:58:06.689+0100 I CONTROL [initandlisten] MongoDB starting : pid=43658 port=27017 dbpath=/data/db 64-bit host=JD10Gen.local
2018-08-28T14:58:06.689+0100 I CONTROL [initandlisten] db version v4.0.2
2018-08-28T14:58:06.689+0100 I CONTROL [initandlisten] git version: fc1573ba18aee42f97a3bb13b67af7d837826b47
2018-08-28T14:58:06.689+0100 I CONTROL [initandlisten] allocator: system


The mongod starts listening on port 27017 by default. As every MongoDB driver
defaults to connecting on localhost:27017 we won’t need to specify a connection string explicitly in these early examples.

Now, we want to work with the Python driver. These examples are using Python
3.6.5 but everything should work with versions as old as Python 2.7 without problems.

Unlike SQL databases, databases and collections in MongoDB only have to be named to be created. As we will see later this is a lazy creation process, and the database and corresponding collection are actually only created when a document is inserted.

$ python
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 03:03:55)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pymongo
>>> client = pymongo.MongoClient()
>>> database = client[ "ep002" ]
>>> people_collection = database[ "people_collection" ]
>>> result=people_collection.insert_one({"name" : "Joe Drumgoole"})
>>> result.inserted_id
>>> result.acknowledged
>>> people_collection.find_one()
{'_id': ObjectId('5b62e6f8c3b498fbfdc1c20c'), 'name': 'Joe Drumgoole'}

First we import the pymongo library (line 6). Then we create the local client proxy object,
client = pymongo.MongoClient() (line 7) . The client object manages a connection pool to the server and can be used to set many operational parameters related to server connections.

We can leave the parameter list to the MongoClient call blank. Remember, the server by default listens on port 27017 and the client by default attempts to connect to localhost:27017.

Once we have a client object, we can now create a database, ep002 (line 8)
and a collection, people_collection (line 9). Note that we do not need an explicit DDL statement.

Using Compass to examine the database server

A database is effectively a container for collections. A collection provides a container for documents. Neither the database nor the collection will be created on the server until you actually insert a document. If you check the server by connecting MongoDB Compass you will see that there are no databases or collections on this server before the insert_one call.

screen shot of compass at start

These commands are lazily evaluated. So, until we actually insert a document into the collection, nothing happens on the server.

Once we insert a document:

>>>> result=database.people_collection.insert_one({"name" : "Joe Drumgoole"})
>>> result.inserted_id
>>> result.acknowledged
>>> people_collection.find_one()
{'_id': ObjectId('5b62e6f8c3b498fbfdc1c20c'), 'name': 'Joe Drumgoole'}

We will see that the database, the collection, and the document are created.

screen shot of compass with collection

And we can see the document in the database.

screen shot of compass with document

_id Field

Every object that is inserted into a MongoDB database gets an automatically
generated _id field. This field is guaranteed to be unique for every document
inserted into the collection. This unique property is enforced as the _id field
is automatically indexed
and the index is unique.

The value of the _id field is defined as follows:


The _id field is generated on the client and you can see the PyMongo generation code in the file. Just search for the def _generate string. All MongoDB drivers generate _id fields on the client side. The _id field allows us to insert the same JSON object many times and allow each one to be uniquely identified. The _id field even gives a temporal ordering and you can get this from an ObjectID via the generation_time method.

>>> from bson import ObjectId
>>> x=ObjectId('5b7d297cc718bc133212aa94')
>>> x.generation_time
datetime.datetime(2018, 8, 22, 9, 14, 36, tzinfo=)
>>> print(x.generation_time)
2018-08-22 09:14:36+00:00

Wrap Up

That is create in MongoDB. We started a mongod instance, created a MongoClient proxy, created a database and a collection and finally made then spring to life by inserting a document.

Next up we will talk more abou Read part of CRUD. In MongoDB this is the find query which we saw a little bit of earlier on in this episode.

For direct feedback please pose your questions on twitter/jdrumgoole that way everyone can see the answers.

The best way to try out MongoDB is via MongoDB Atlas our Database as a Service.
It’s free to get started with MongoDB Atlas so give it a try today.

Full Stack Hack – A Learning Hackathon

Screen Shot 2018-04-20 at 10.08.47

Next Thursday (26th April) and Friday (27th April) we are running a Full Stack Hackathon in London. The goal of the Hackathon is to give you, the developer a chance to work with three of the most compelling technologies for building modern Applications, specifically Node.js,  Apacha Kafka and MongoDB.

Why these three technologies? Well, they are all at the forefront of the revolution in MicroServices and scalable web architectures, they are all Open Source and they all put the JSON data format front and centre of their design.

At 7.00pm on Thursday we will all get together at 15 Hatfields in London to start building teams.


Each team can have a minimum of two and a maximum of 5 team members. If you want to build a team and you have an idea, this is the time to pitch your ideas. Focus on the idea of a Minimum Viable Product.

Remember you only have Friday to build your app. There will be plenty of pizza beer and soft-drinks on hand to ensure no one goes hungry or thirsty. Everyone who turns up will get swag bag of goodies from all the vendors including tee-shirts, pens, stickers etc. etc.

On Friday we start bright and early.  The team will be there from 8.00an but we expect people to arrive at 9.00am. We will provide breakfast rolls, tea and coffee and we will refresh the pitches from last night. This is when we finalise the teams for the day.

After that its full speed hacking until 4.30pm. There will be a break for lunch which we will provide and there will be snacks and soft-drinks available throughout the day.

There will be experts on hand from Nearform, MongoDB and Confluent to help you with your hacking challenge.

At 4.30pm, each team will pitch and demo their projects. Each team gets 5 minutes. Then myself, Tim Berglund and Conor O’Neill will decide on a winner.  Each member of the winning team will receive an Amazon Echo Dot.

What will the winning team look like? They will have used all the technologies in a compelling way to build a minimum viable product that blows our socks off.

Once prize giving is done we will retire to a local pub to celebrate the day with craft beer. We are buying!

All this for the low low fee of £10.  Register now, spaces are limited. We can’t wait to see you on the day.




The Irish Internet Association Annual Conference, May 12th Aviva Stadium

In May 12th the Irish Internet Association will hold Open for Business: The IIA Annual Conference 2011 in the Aviva Stadium in Dublin.

The programme for this key industry conference on May 12th in the Aviva Stadium has been finalised. It features among others  

  • Richard Bruton T.D., Minister for Enterprise, Jobs and Innovation;
  • Dylan Collins, Founder, Jolt Online Games;
  • Josh Holmes, Architecture Evangelist, Microsoft 
  • Constantin Gurdgiev, Macroeconomist, IBM;
  • Louise Phelan, Senior Director, PayPal;
  • Tim Willoughby, Local Government Computer Services Board;
  • and many more.

Commenting on this unique internet business event, Joan Mulvihill, IIA CEO said, “To acknowledge thebreadth of our membership we have created two conferences in one: the morning plenary sessionsfocus on Irish businesses who are consuming, commissioning and using technology to achieve their business goals while the afternoon focuses on those who are creating and crafting technologies for them as well as workshops for both.”

The term ‘conference’ is being taken literally by the IIA! 

con·fer·ence (kOn’for-ens)

n.1. a. A meeting for consultation or discussion. b. An exchange of views.

The theme ‘Open for Business’ was chosen to reflect an open ethos of sharing and communication – an authentic conversation for all internet stakeholders.

The optimisation of new technologies is key to staying open for business. The internet is the recognised driver in growing salesmanaging risk and reducing costs.

Being ‘open for business’ is key to economic recovery and knowledge and skills must be in place to deliver. The morning panel discussion will assess the scale of the current skills deficit in delivering a smart economy.

However, to technologists the meaning of OPEN is entirely different and at times sparks opposing views  as ‘Open’ typically refers to open source or open data.

The possibilities for an open government, facilitated by technology will be examined with case study presentations from Fingal County Council and an examination of the issues from Tim Willoughby, Assistant Director of the Local Government Computer Services Board. This is a unqiue opportunity for government bodies and web developers to engage in an open conversation in the hope of kickstarting change.

In parallel, throughout the day, breakout sessions will be run allowing delegates to hone in on particular topics – from cloud computing to digital marketing and ecommerce – ideal for all business owner/managers. The morning breakout conversations will be on the topics of Open (data/source/government).  The plan is to share ideas and garner a collective view on key industry issues so that this conference is part of an overall process for progress.

The complete programme is detailed below. If you would like to interview any of the speakers or IIA CEO Joan Mulvihill please contact IIA Communications Manager Roseanne Smith at or 087 222 3614.

The programme includes a full day of plenary sessions as well as parallel break-out sessions and clinics.

  • 8.30am Registration
  • 9.00am Chairperson Address Maeve Kneafsey, IIA Chair
  • 9.15am eCommerce Keynote address: Louise  Phelan, Senior Director Global Customer Services & EU Merchant Services at PayPal
  • 10.00am Entrepreneurship Keynote address “Being an Internet Entrepreneur”
    Dylan Collins, Founder, Jolt Online Gaming and 2010 IIA & Enterprise Ireland Net Visionary
  • 10.45am Q&A
  • 11.00am Break
  • 11.15am Panel Debate “Have we the smarts?” (a critique of Ireland’s skills deficit for a smart economy)
    – Colm Greally (Mobile Entrepreneur, DigitalReachGroup)
    – Theo Lynn (Director LINK Research Centre and Industry Engagement, DCU)
    – Constantin Gurdyiev (Economist)
    – Paddy Cosgrave, Dublin Web Summit
  • Panel Moderator – Ann O’Dea, Editor in Chief, Business and Leadership
  • 12.15pm Keynote Richard Bruton, T.D. Minister for Enterprise, Jobs and Innovation
  • 1pm Lunch
  • 2.15pm Board Address, “Open for Business” Maurice Mortell, IIA Board & CEO, DataElectronics
  • 2.30pm Open Source – Josh Holmes, Microsoft “Deep Fried Open-Source”
  • 3.00pm Keynote Address, “Open-Data for Open-Gov” Tim Willoughby, Assistant Director, Local Government Computer Services Board
  • 3.30pm Q&A
  • 3.45pm Break
  • 4.00pm Panel Debate Report from conversation break-out leaders
    – National Digital Research Centre – Amy Neale
    – Irish Software Association – Sean Baker
    – Neil Leyden, Winner of Your Country, Your Call.
    – Dublin City Council – Peter Finnegan, Director International Relations & Research
  • 5.30pm Close