MongoDB

Like an RDBMS

  • uses a client and a server
  • cross-language access
In [4]:
%%script mongo
show dbs
MongoDB shell version: 2.4.6
connecting to: test
bronto	7.9501953125GB
five	0.203125GB
google_analytics	19.9443359375GB
local	0.078125GB
netsuite	3.9521484375GB
small	0.203125GB
test	0.203125GB
v	0.203125GB
bye
In [3]:
%%script mongo dayton
db.dropDatabase()
MongoDB shell version: 2.4.6
connecting to: dayton
{ "dropped" : "dayton", "ok" : 1 }
bye

Different jargon

  • Database -> database
  • Table (or relation) -> collection
  • Row (or tuple) -> document

Schemaless: Mentioning it is creating it

Handy and terrifying

In [5]:
%%script mongo
use dayton;
db.members.insert({'name': 'Ken', 'language': 'Perl'})
MongoDB shell version: 2.4.6
connecting to: test
switched to db dayton
bye
In [8]:
%%script mongo
show dbs;
use dayton;
show collections;
MongoDB shell version: 2.4.6
connecting to: test
bronto	7.9501953125GB
dayton	0.203125GB
five	0.203125GB
google_analytics	19.9443359375GB
local	0.078125GB
netsuite	3.9521484375GB
small	0.203125GB
test	0.203125GB
v	0.203125GB
switched to db dayton
members
system.indexes
bye
In [9]:
%%script mongo
use dayton;
db.members.find()
MongoDB shell version: 2.4.6
connecting to: test
switched to db dayton
{ "_id" : ObjectId("528418c9b8fa004981577216"), "name" : "Ken", "language" : "Perl" }
bye

Structure in documents

In [10]:
%%script mongo dayton
db.members.insert({name: "Grant", language: ["PHP", "Perl"]})
db.members.find()
MongoDB shell version: 2.4.6
connecting to: dayton
{ "_id" : ObjectId("528418c9b8fa004981577216"), "name" : "Ken", "language" : "Perl" }
{ "_id" : ObjectId("52841a211de978769fc0ce4c"), "name" : "Grant", "language" : [  "PHP",  "Perl" ] }
bye
In [11]:
%%script mongo dayton
bob = {name: "Bob", language: "Python", 
       spouse: {"name": ""},
       pets: [{"name": "Fido", "species": "dog"}, 
                {"name": "Sonic", "species": "hedgehog"}]
       };
db.members.insert(bob)
MongoDB shell version: 2.4.6
connecting to: dayton
{
	"name" : "Bob",
	"language" : "Python",
	"spouse" : {
		"name" : ""
	},
	"pets" : [
		{
			"name" : "Fido",
			"species" : "dog"
		},
		{
			"name" : "Sonic",
			"species" : "hedgehog"
		}
	]
}
bye
In [12]:
%%script mongo dayton
db.members.find()
MongoDB shell version: 2.4.6
connecting to: dayton
{ "_id" : ObjectId("528418c9b8fa004981577216"), "name" : "Ken", "language" : "Perl" }
{ "_id" : ObjectId("52841a211de978769fc0ce4c"), "name" : "Grant", "language" : [  "PHP",  "Perl" ] }
{ "_id" : ObjectId("52841a91b57f8e18a0ac79a7"), "name" : "Bob", "language" : "Python", "spouse" : { "name" : "" }, "pets" : [ 	{ 	"name" : "Fido", 	"species" : "dog" }, 	{ 	"name" : "Sonic", 	"species" : "hedgehog" } ] }
bye

finding

In [17]:
%%script mongo dayton
db.members.find({pets: [ 	{ 	"name" : "Fido", 	"species" : "dog" }, 	{ 	"name" : "Sonic", 	"species" : "hedgehog" } ]});
MongoDB shell version: 2.4.6
connecting to: dayton
{ "_id" : ObjectId("52841a91b57f8e18a0ac79a7"), "name" : "Bob", "language" : "Python", "spouse" : { "name" : "" }, "pets" : [ 	{ 	"name" : "Fido", 	"species" : "dog" }, 	{ 	"name" : "Sonic", 	"species" : "hedgehog" } ] }
bye

indexing

In [18]:
%%script mongo dayton
db.members.find({name: "Grant"}).explain();
MongoDB shell version: 2.4.6
connecting to: dayton
{
	"cursor" : "BasicCursor",
	"isMultiKey" : false,
	"n" : 1,
	"nscannedObjects" : 3,
	"nscanned" : 3,
	"nscannedObjectsAllPlans" : 3,
	"nscannedAllPlans" : 3,
	"scanAndOrder" : false,
	"indexOnly" : false,
	"nYields" : 0,
	"nChunkSkips" : 0,
	"millis" : 0,
	"indexBounds" : {
		
	},
	"server" : "elli:27017"
}
bye
In [20]:
%%script mongo dayton
db.members.ensureIndex({pets: 1});
db.members.find({name: "Grant"}).explain();
MongoDB shell version: 2.4.6
connecting to: dayton
{
	"cursor" : "BtreeCursor name_1",
	"isMultiKey" : false,
	"n" : 1,
	"nscannedObjects" : 1,
	"nscanned" : 1,
	"nscannedObjectsAllPlans" : 1,
	"nscannedAllPlans" : 1,
	"scanAndOrder" : false,
	"indexOnly" : false,
	"nYields" : 0,
	"nChunkSkips" : 0,
	"millis" : 0,
	"indexBounds" : {
		"name" : [
			[
				"Grant",
				"Grant"
			]
		]
	},
	"server" : "elli:27017"
}
bye

updating

danger!

In [21]:
%%script mongo dayton
db.members.find({name: "Bob"})
MongoDB shell version: 2.4.6
connecting to: dayton
{ "_id" : ObjectId("52841a91b57f8e18a0ac79a7"), "name" : "Bob", "language" : "Python", "spouse" : { "name" : "" }, "pets" : [ 	{ 	"name" : "Fido", 	"species" : "dog" }, 	{ 	"name" : "Sonic", 	"species" : "hedgehog" } ] }
bye
In [22]:
%%script mongo dayton
db.members.update({name: "Bob"}, {cm: 180});
MongoDB shell version: 2.4.6
connecting to: dayton
bye
In [23]:
%%script mongo dayton
db.members.find();
MongoDB shell version: 2.4.6
connecting to: dayton
{ "_id" : ObjectId("528418c9b8fa004981577216"), "name" : "Ken", "language" : "Perl" }
{ "_id" : ObjectId("52841a211de978769fc0ce4c"), "name" : "Grant", "language" : [  "PHP",  "Perl" ] }
{ "_id" : ObjectId("52841a91b57f8e18a0ac79a7"), "cm" : 180 }
bye

Note that, by default, only one document is updated. Fourth arg = true enables multi-update.

In [31]:
%%script mongo dayton
db.members.update({name: "Grant"}, {$set: {cm: 180}}, true, true);
MongoDB shell version: 2.4.6
connecting to: dayton
bye
In [32]:
%%script mongo dayton
db.members.find()
MongoDB shell version: 2.4.6
connecting to: dayton
{ "_id" : ObjectId("528418c9b8fa004981577216"), "name" : "Ken", "language" : "Perl" }
{ "_id" : ObjectId("52841a91b57f8e18a0ac79a7"), "cm" : 180 }
{ "_id" : ObjectId("52841a211de978769fc0ce4c"), "cm" : 180, "language" : [  "PHP",  "Perl" ], "name" : "Grant" }
bye

need the $set modifier

In [ ]:
%%script mongo dayton
db.members.update({name: "Ken"}, {$set: {cm: 185}});
db.members.find({name: "Ken"});
In [ ]:
%%script mongo dayton
db.members.remove({name: null})
db.members.find()

From your language

In [33]:
import pymongo
client = pymongo.Connection()
In [34]:
db = client.dayton
db
Out[34]:
Database(Connection('localhost', 27017), u'dayton')
In [35]:
db.members.find()
Out[35]:
<pymongo.cursor.Cursor at 0x32a2a50>
In [36]:
for member in db.members.find():
    print member
{u'_id': ObjectId('528418c9b8fa004981577216'), u'name': u'Ken', u'language': u'Perl'}
{u'_id': ObjectId('52841a91b57f8e18a0ac79a7'), u'cm': 180.0}
{u'_id': ObjectId('52841a211de978769fc0ce4c'), u'language': [u'PHP', u'Perl'], u'cm': 180.0, u'name': u'Grant'}
In [37]:
dave = {'name': 'Dave', 'language': 'Python'}
db.members.insert(dave)
list(db.members.find())
Out[37]:
[{u'_id': ObjectId('528418c9b8fa004981577216'),
  u'language': u'Perl',
  u'name': u'Ken'},
 {u'_id': ObjectId('52841a91b57f8e18a0ac79a7'), u'cm': 180.0},
 {u'_id': ObjectId('52841a211de978769fc0ce4c'),
  u'cm': 180.0,
  u'language': [u'PHP', u'Perl'],
  u'name': u'Grant'},
 {u'_id': ObjectId('52842205417f34189f8d7f61'),
  u'language': u'Python',
  u'name': u'Dave'}]

no joins

two-query method

In [38]:
%%script mongo dayton
db.languages.insert({_id: "Ruby", creator: "Matz"});
db.languages.insert({_id: "Python", creator: "Guido"});
db.languages.insert({_id: "Perl", creator: "Larry"});
db.languages.find()
MongoDB shell version: 2.4.6
connecting to: dayton
{ "_id" : "Ruby", "creator" : "Matz" }
{ "_id" : "Python", "creator" : "Guido" }
{ "_id" : "Perl", "creator" : "Larry" }
bye
In [39]:
for member in db.members.find():
    print member.get('name')
    if 'language' in member:
        print member['language']
        lang = db.languages.find_one({'_id': member['language']})
        if lang:
            print lang['creator']
    print
Ken
Perl
Larry

None

Grant
[u'PHP', u'Perl']

Dave
Python
Guido

nesting

In [40]:
%%script mongo dayton
db.members.update({language: "Python"}, {$set: {language: {name: "Python", creator: "Guido"}}})
db.members.update({language: "Ruby"}, {$set: {language: {name: "Ruby", creator: "Matz"}}})
db.members.update({language: "Perl"}, {$set: {language: {name: "Perl", creator: "Larry"}}})
db.members.find()
MongoDB shell version: 2.4.6
connecting to: dayton
{ "_id" : ObjectId("52841a91b57f8e18a0ac79a7"), "cm" : 180 }
{ "_id" : ObjectId("52841a211de978769fc0ce4c"), "cm" : 180, "language" : [  "PHP",  "Perl" ], "name" : "Grant" }
{ "_id" : ObjectId("52842205417f34189f8d7f61"), "language" : { "name" : "Python", "creator" : "Guido" }, "name" : "Dave" }
{ "_id" : ObjectId("528418c9b8fa004981577216"), "language" : { "name" : "Perl", "creator" : "Larry" }, "name" : "Ken" }
bye
In [43]:
for member in db.members.find():
    if 'name' in member:
        print member['name']
        if 'language' in member:
            if not isinstance(member['language'], list):
                print member['language']['creator']
Grant
Dave
Guido
Ken
Larry
  • 16 MB record limit
  • redundant storage
In [ ]:
%%script mongo dayton
db.dropDatabase()