Skip to content

Instantly share code, notes, and snippets.

@rtoal
Last active December 17, 2015 03:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rtoal/5542980 to your computer and use it in GitHub Desktop.
Save rtoal/5542980 to your computer and use it in GitHub Desktop.
Transcript for an intermediate-level mongo demonstration
# Mongo not happy unless lots of open files
ulimit -n 2000
# Start server
sudo mongod --fork --syslog
# Already done
unzip all-films.zip
mongoimport -d mgo -c films all-films.json
# Review the basics through a client
mongo
show dbs
use mgo
show collections
db.films.findOne()
db.films.count()
db.films.find({}).limit(5).pretty()
db.films.find({box_office_cume:{$gt:2000000}})
db.films.find({
"titles.name":{$regex:/Matrix/},
box_office_cume:{$gt:150000000}})
db.films.find({countries:"South Africa"},
{_id:0, "titles.name":1})
db.films.find({"ratings.value":"G"},
{titles:{$elemMatch:{main:true}}}).forEach(function (d)
{print(d._id + "|" + d.titles[0].name)})
db.films.aggregate([
{$unwind : "$countries"},
{$group : {_id : "$countries", number: {$sum : 1}}},
{$match: {number: {$lt:500, $gt:200}}},
{$sort : {number : -1}},
])
db.films.aggregate([
{$project: {box: "$box_office_cume"}},
{$group : {_id : null, amount: {$sum : "$box"}}}
])
db.films.aggregate([
{$project: {_id: "$_id", awards: "$awards"}},
{$unwind: "$awards"},
{$match: {
"awards.result": "Winner",
"awards.award_group": {$regex: /^Golden Globe/i},
"awards.award_category": {$regex: /Actress/i}
}},
{$limit: 10},
])
# Must enable text search
$ mongod --setParameter textSearchEnabled=true --syslog --fork
mongo
db.films.ensureIndex({'titles.name':'text', 'images.caption':'text'})
db.films.getIndexes()
db.films.runCommand("text", {search: "matrix"})
db.films.runCommand("text", {search: "run", project:{'titles.name':1}, limit:5})
db.films.runCommand("text", {search: "matrix", project:{'titles.name':1,'images.caption':1,_id:0}})
# Back out to shell - Replication demo time!
mkdir 1 ; mkdir 2 ; mkdir 3
mongod --replSet abc --dbpath 1 --port 27001 --oplogSize 50 --logpath log.1 \
--logappend --fork
mongod --replSet abc --dbpath 2 --port 27002 --oplogSize 50 --logpath log.2 \
--logappend --fork
mongod --replSet abc --dbpath 3 --port 27003 --oplogSize 50 --logpath log.3 \
--logappend --fork
cat log.1
ps -ef | grep mongo
# Go to the primary
mongo --port 27001
> rs.help()
> rs.status()
> cfg = {_id: 'abc', members: [
{_id: 0, host: 'localhost:27001'},
{_id: 1, host: 'localhost:27002'},
{_id: 2, host: 'localhost:27003'}]}
> rs.initiate(cfg)
> rs.status() # run a few times until all up :)
abc:PRIMARY> rs.status()
abc:PRIMARY> rs.conf()
abc:PRIMARY> rs.conf
abc:PRIMARY> db.isMaster()
abc:PRIMARY> show dbs
# DO THIS IN A SECOND WINDOW
$ mongo --port 27002
# Back to first window
db.shows.insert({
_id: 1,
name: 'Max Headroom'})
# BACK TO SECOND WINDOW
abc:S> db.shows.findOne()
abc:S> rs.slaveOk()
abc:S> db.shows.findOne()
abc:S> db.isMaster()
abc:S> rs.status()
abc:S> db.shows.insert({name:'Cheers'})
# SHARDING
See gist 5494049
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment