Mongo Db

Overview

  • You dont need to create users / passwords / dbs or tables just use it
  • Programming language is Javascript

List all databases

show dbs

Connect to a db

use <db>

List all collections

show collections

Insert data

db.<collection>.save({name: "wurst", firstname: "hans"})
  • Import in json format
cat json_file | /usr/bin/mongoimport --type json -d mydb -c mycollection

Select from collection

  • all
db.<collection>.find()
  • filter by field
db.<collection>.find(field: "value")
  • filter by regexp
db.<collection>.find(field: /\d+/)
  • select specific fields
db.<collection>.find({},{"field_to_select": 1})

Iterate over all results

var cursor = db.<collection>.find();
while (cursor.hasNext()) printjson(cursor.next());
  • Or better
db.<collection>.find().forEach(printjson)

Sorting

  • Lowest first
db.<collection>.find().sort({"field": 1})
  • Highest first
db.<collection>.find().sort({"field": -1})

Update data

db.<collection>.update({"_id": 1}, {$set: {"field": "new value"}})

Delete data

  • Remove complete collection
db.<collection>.drop()
  • Remove some entries
db.<collection>.remove({"name": "wurst"})
  • Delete a field

    db.<collection>.update({“_id”: 1}, {$unset: {“field”: “”}})


  • Delete whole database
db.dropDatabase()

Working with timestamps

  • Get all entries from 1.12. till 6.12.
db.snmp.find({'time': {'$gt': ISODate('2012-12-01T00:00:00'), '$lt': ISODate("2012-12-05T23:59:59")}})

Select distinct values

db.<collection>.distinct('field')

Create index on collection field

  • sparse will only create index if a value for that field exists
db.<collection>.ensureIndex( { myfield: 1 }, {sparse: true, background: true} );
  • Unique constraint
db.<collection>.ensureIndex( { myfield: 1 }, {unique: true} );

Show indexes

  • Of collection
db.<collection>.getIndexes()
  • All
db.system.indexes.find()

Using references

some_field: new DBRef('collection_of_target', target_document._id)

User administration

  • To setup authentication edit /etc/mongodb.conf and set
auth = true
  • Now execute the following in mongo shell
use <db>
db.addUser({user: "foo", pwd: "bar", roles: ["readWrite", "dbAdmin"]})
  • use role read for read-only access
  • role root will give overall access
  • other useful roles “userAdminAnyDatabase”, “readWriteAnyDatabase”, “dbAdminAnyDatabase” (use db admin in this case)
  • give clusterAdmin roles if user should be able to configure clustering
  • To log into a database
db.auth("user", "pass")
  • List all users
db.getUsers()
  • List one user
db.getUser("foo")
  • To change a users password
db.changeUserPassword("user", "newpassword")
  • Update a users roles
db.grantRolesToUser("vip_user", ["userAdminAnyDatabase", "readWriteAnyDatabase", "dbAdminAnyDatabase", "clusterAdmin"])

Replication

  • All nodes share the same data
  • You need at least 3 server
  • Edit /etc/mongodb.conf and set a replSet
replSet = rs0
  • On the master node execute the folowing in mongo shell
rs.initiate()
rs.conf()
rs.add("mongodb1.example.net")
rs.add("mongodb2.example.net")
rs.status()
  • You can define one as master and the others as slaves

Clustering / Sharding

  • Sharding means to spread the database horizontally across multiple servers
  • config servers store the complete metadata of a cluster (you should have 3)
mongod --configsvr --dbpath <path>
  • mongos are query servers (configdb must define configsvr nodes in same order!)
mongos --configdb node1:27019,node2:27019,node3:27019
  • shard servers store the data
mongo --host querysrv --port 27017
  • You can add the new shard to a replica set
sh.addShard( "rs0/newnode:27017" )
  • or to a whole
sh.addShard( "newnode:27017" )
  • You can set configsvr or shardsvr=true in /etc/mongodb.conf to make the server a config, query or shard server
  • configdb = <cfgsrv1>, <cfgsrv2>, <cfgsrv3>
  • To enable sharding for a database run
sh.enableSharding("<db>")
sh.status()

Backup

#!/bin/bash

$BACKUP_DIR="/my/backup/path"

cd $BACKUP_DIR
/usr/bin/mongodump

/bin/tar cvjf `hostname`_`date +%Y%m%d`.tbz dump && /bin/rm -rf $BACKUP_DIR/dump

Show real data size

  • With indizes
db.<collection>.totalSize()
  • Only data
db.<collection>.dataSize()

Defrag

  • Whole database
db.repairDatabase()
  • Single collection
db.<collection>.compact()

Load collection into memory

db.runcommand({ touch: “collection_name”, data: true, index: true})

Round robin collections

If you’ve not heard of capped collections before, they’re a nice little feature of MongoDB that lets you have a high-performance circular queue. Capped collections have the following nice features:

  • They “remember” the insertion order of their documents
  • They store inserted documents in the insertion order on disk
  • They remove the oldest documents in the collection automatically as new documents are inserted
db.create_collection(
  'capped_collection',
  capped=True,
  size=size_in_bytes,     # required
  max=max_number_of_docs, # optional
  autoIndexId=False)      # optional
  • One can tail for new data in capped collections
cur = db.capped_collection.find(
      tailable=True,
      await_data=True)

Troubleshooting

  • I always get not master errors
rs.status()
rs.initiate()
  • Or if you really want to access a slave node
db.setSlaveOk()

Getting help

db.help()