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.

PyMongo Monday: Episode 1: Setting Up Your PyMongo Environment

Front Square, Trinity College, Dublin

Welcome to PyMongo Monday. This is the first in a series of regular blog posts that will introduce developers to programming MongoDB using the Python programming language. It’s called PyMongo Monday because PyMongo is the name of the client library (in MongoDB speak we refer to it as a “driver”) we used to interact with the MongoDB Server. Monday because we aim to release each new episode on Monday.

To get started we need to install the toolchain that a typical MongoDB Python developer would expect to use.

Installing m

First up is m. Hard to find online unless your search for MongoDB m, m is a tool to manage and use multiple installations of the MongoDB Server in parallel. It is an invaluable tool if you want to try out the latest and greatest beta version but still continue mainline development on our current stable release.

The easiest way to install m is with npm the Node.js package manager (which it turns out is not just for Node.js).

$ npm install -g m
/usr/local/bin/m -> /usr/local/lib/node_modules/m/bin/m
+ m@1.4.1
updated 1 package in 2.361s

If you can’t or don’t want to use npm you can download and install directly from the github repo. See the README there for details.

For today we will use m to install the current stable production version (4.0.2 at the time of writing).

We run the stable command to achieve this.

$ m stable
MongoDB version 4.0.2 is not installed.
Installation may take a while. Would you like to proceed? [y/n] <b>y</b>
... installing binary

######################################################################## 100.0%
... removing source
... installation complete

If you need to use the path directly in another program you can get that with m bin.

$ m bin 4.0.0

To run the corresponding binary do m use stable

$ m use stable
2018-08-28T11:41:48.157+0100 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2018-08-28T11:41:48.171+0100 I CONTROL [initandlisten] MongoDB starting : pid=38524 port=27017 dbpath=/data/db 64-bit host=JD10Gen.local
2018-08-28T11:41:48.171+0100 I CONTROL [initandlisten] db version v4.0.2
2018-08-28T11:41:48.171+0100 I CONTROL [initandlisten] git version: fc1573ba18aee42f97a3bb13b67af7d837826b47
<b><i>&lt other server output &gt</i></b>
2018-06-13T15:52:43.648+0100 I NETWORK [initandlisten] waiting for connections on port 27017

Now that we have a server running we can confirm that it works by connecting via the
mongo shell.

$ mongo
MongoDB shell version v4.0.0
connecting to: mongodb://
MongoDB server version: 4.0.0
Server has startup warnings:
2018-07-06T10:56:50.973+0100 I CONTROL [initandlisten]
2018-07-06T10:56:50.973+0100 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-07-06T10:56:50.973+0100 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2018-07-06T10:56:50.973+0100 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-07-06T10:56:50.973+0100 I CONTROL [initandlisten]
2018-07-06T10:56:50.973+0100 I CONTROL [initandlisten] ** WARNING: This server is bound to localhost.
2018-07-06T10:56:50.973+0100 I CONTROL [initandlisten] ** Remote systems will be unable to connect to this server.
2018-07-06T10:56:50.973+0100 I CONTROL [initandlisten] ** Start the server with --bind_ip &lt address&gt to specify which IP
2018-07-06T10:56:50.973+0100 I CONTROL [initandlisten] ** addresses it should serve responses from, or with --bind_ip_all to
2018-07-06T10:56:50.973+0100 I CONTROL [initandlisten] ** bind to all interfaces. If this behavior is desired, start the
2018-07-06T10:56:50.973+0100 I CONTROL [initandlisten] ** server with --bind_ip to disable this warning.
2018-07-06T10:56:50.973+0100 I CONTROL [initandlisten]

Enable MongoDB's free cloud-based monitoring service to collect and display
metrics about your deployment (disk utilization, CPU, operation statistics,

The monitoring data will be available on a MongoDB website with a unique URL created for you. Anyone you share the URL with will also be able to view this page. MongoDB may use this information to make product improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()


These warnings are standard. They flag that this database has no access controls set up by default andthat it is only listening to connections coming from the machine it is running on (localhost). We will learn how to set up access control and listen on a broader range of ports in later episodes.

Installing the PyMongo Driver

But this series is not about the MongoDB Shell, which uses JavaScript as its coin of the realm, it’s about Python. How do we connect to the database with Python?

First, we need to install the MongoDB Python Driver, PyMongo. In MongoDB parlance a driver is a language-specific client library used to allow developers to interact with the server in the idiom of their own programming language.

For Python that means the driver is installed using pip. In node.js the driver is installed using npm and in Java you can use maven.

$ pip3 install pymongo
Collecting pymongo
Downloading (333kB)
100% |████████████████████████████████| 337kB 4.1MB/s
Installing collected packages: pymongo
Successfully installed pymongo-3.7.1

We recommend you use a virtual environment to isolate your PyMongo Monday code. This is not required but is very convenient for isolating different development streams.

Now we can connect to the database:

$ 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(host="mongodb://localhost:8000")
>>> result = client.admin.command("isMaster")
>>> import pprint
>>> pprint.pprint(result)
{'ismaster': True,
'localTime': datetime.datetime(2018, 6, 13, 21, 55, 2, 272000),
'logicalSessionTimeoutMinutes': 30,
'maxBsonObjectSize': 16777216,
'maxMessageSizeBytes': 48000000,
'maxWireVersion': 6,
'maxWriteBatchSize': 100000,
'minWireVersion': 0,
'ok': 1.0,
'readOnly': False}

First we import the PyMongo library (line 5). The we create a local client object (line 6) that holds the connection pool and other status for this server. We generally don’t want more than one MongoClient object per program as it provides its own connection pool.

Now we are ready to issue a command to the server. In this case its the standard MongoDB server information command which is called rather anachronistically isMaster (line 7). This is a hangover from the very early versions of MongoDB. It appears in pre 1.0 versions of MongoDB ()which is over ten years old at this stage).

The isMaster command returns a dict which details a bunch of server information. In order to format this in a more readable way import the pprint library.

That’s the end of episode one. We have installed MonogDB, installed the Python client library (aka driver),started a mongod server and established a connection between the client and server.

Next week we will introduce CRUD operations on MongoDB starting with Create.

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. You can deploy a free cluster without giving us a credit card.

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.




Connecting to MongoDB Atlas

Atlas is the MongoDB database as a service offering. With Atlas, you can create fully managed MongoDB databases on AWS, Google Cloud, and Azure. If you have been playing around with MongoDB locally you will know how trivially easy it is for clients and servers to connect.

Servers listen by default on port 27017 and clients by default expect to connect to localhost:27017. With Atlas, we need to connect to a remote server that expects both a username and password and an SSL connection.  But don’t worry Atlas makes this super easy to configure.

First login to your Atlas cluster at Here is my login page.

Screen Shot 2018-03-22 at 09.49.15

Now click the connect button. This will pop up the following screen.

Screen Shot 2018-03-22 at 09.49.31

You want to scroll down to the Connect Your Application and click there.

This will open up the Connection string screen.

Screen Shot 2018-03-22 at 09.49.46

You now need to decide whether you are using the latest drivers (3.6) or an earlier version. When you originally created the cluster you selected a specific version of the server on the Create cluster page.

Screen Shot 2018-03-22 at 10.18.26

If you are using a 3.6 driver then the server will be configured to use the new seedlist configuration format.

If you click on that link you will get a window displaying the correct connection string.

Screen Shot 2018-03-22 at 09.50.01.png

This string is hard to read on the screenshot so we can reproduce it here.


Screen Shot 2018-03-22 at 09.50.06

For the 3.4 and earlier drivers, we use the old format MongoDB URI connection string.



In both cases, you will need to supply the password that you created for your user. Note this is the password for the database, not your Atlas login password.

Bitcoin Bubble?


Around Christmas last year (2016) I was doing some internal training on Bitcoin and Blockchain for sales staff at MongoDB. This was nothing too heavy but as part of the process I reckoned that if I was going to understand this thing I would buy some as part of the process. I bought two tranches of €100, the first in December 2016 and the second (just before a talk at UCD on the same topic) in January 2017. There was a €3 transaction fee for each purchase.

I did occasionally look at the Bitcoin prices and watched its ups and downs. Eventually when it seemed to be at an all time high in May 2016 I cashed out my original €200. I kind of forgot about the residue until last night. I checked the price around 12pm.

€388 euro.

Feels bubblish to me 🙂

Screen Shot 2017-08-30 at 08.54.40

The Ultimate MVP : The Saturn 5 Rocket

Saturn 5 Rocket
Saturn 5 on its Launch pad

Most people in larger companies look at the lean-startup movement and think it doesn’t apply to them (despite much of its inspiration coming from a very large company, Toyota). Well NASA ran a very successful Minimum Viable Product through the 1960’s called the Apollo program.

The Apollo program had a simple goal, put a man on the moon.  Every part of the mission was dedicated to that end. So although the Saturn V rocket is the largest rocket every built it was completely disposable apart from the Command Module that returned to Earth with the astronauts. Even that was discarded once the astronauts had been recovered.

There was no attempt to design reuse for trips to other planets or reuse in the mission of space station building. They reused extensive design knowledge gained in the design of Mercury and  Gemini and space missions and focussed on design simplicity and remote monitoring as opposed to onboard maitainance to resolve problems.

Most importantly, they recognized that the short duration of Apollo missions meant that the parts and sub-systems did not need to undergo the rigor of prolonged testing in the harsh environment of Space.

They wouldn’t have called it that at the time but they were engaging in lean design and adopting lean startup principles.

  • Minimum Viable Product: A rocket to put a man on the moon and bring him back
  • Reuse of standard components: Many of the sub-systems for Gemini and Mercury were carried straight across
  • No Over design: Design for the current mission, a short duration trip to the Moon, no a long duration flight to Mars.
  • Rapid Iteration: 11 Apollo missions in 8 years culminating in Apollo 11 that put a man on the moon
  • Failed Experiments:  Apollo 1 resulted in a mission fire that killed all three astronauts, apollo

So next time someone says MVP is a the new new thing. Tell ’em about the Apollo program.

My Every Day Electronics Carry


  1. US USB power adaptor
  2. Ethernet connector for MacBook Air (so far never used but its only been a month)
  3. US plug adaptor for Apple power block
  4. VGA dongle for Mac (amazingly this is the first one I bought and I still have it)
  5. USB to USB connector (male to female)
  6. USB to printer form factor connector
  7. USB dongle with attachment to allow it to read/write MicroSD cards
  8. Another USB form factor convertor
  9. Universal plug adaptor (works everywhere). Got this in Frys. Its great ‘cos it has a USB power point built in
  10. Three MIFI
  11. Mac iPad Cable (also can be used to charge iPhones)
  12. MicroUSB cable
  13. Verizon LTE MIFI (for USA)
  14. USB extension cable
  15. USB hub
  16. Ethernet (use to roll up but the spring is broken)
  17. MicroUSB car charger

Not shown is my MacBook Air, my Samsung Galaxy G4 and their associated chargers. I carry all this stuff in a transparent Ziplock bag so I can easily see what I am looking for. My backpack of choice (not shown) is a Lowe Alpine computer backpack, I favour it because it comes with a slip on waterproof cover which is great for cycling in Irish weather.

Why I am Joining 10gen (The MongoDB Company)

Monday will mark my first day at 10gen as Director EMEA. Why leave a very successful Irish startup, FeedHenry for a new position in 10gen I hear you ask?

Well, it’s not everyday you get a opportunity to work for a company that is changing the world of Enterprise Data, Hugh McLeod famously challenged Microsoft to “Change The World or Go Home” and that’s exactly what 10gen is doing with MongoDB. With 4 million downloads and counting and enormous credibility amongst the code cutters who actually build software everyday this is a once in a lifetime opportunity.

Its also Open Source which is something I have been passionate about ever since installing my first GNU C compiler in 1989.

What will I be doing for 10gen? I think my boss Ron Avnur described it best: my job will be to help 10gen customers become successful using MongoDB.

I will continue to be a booster for FeedHenry and I wish everyone in that company the best of success.