Getting Started
Using homebrew on OS X | With homebrew, simplybrew install mongodb |
Download |
|
Symbolic Link | Easiest way to eliminate issues when upgrading to new versions over time:
ln -s mongodb-osx-x86_64-1.6.5 mongodb Then you can simply make scripts that are “generic”: /runtimes/mongodb/mongodb/bin/mongorestore --drop ~/workprojects/myproject/dump/ |
Getting Started | Getting started – also describes template approach |
Another example | Getting started with a new project – also describes YAML config |
Start/Stop
Start DB
as root:
/mongodb/mongodb/bin/mongod --dbpath /mongodb/data/db
As daemon:
sudo /mongodb/mongodb/bin/mongod --fork --dbpath /mongodb/data/db
Shutdown via console shell
> use admin switched to db admin > db.shutdownServer()
With logger:
sudo /mongodb/mongodb/bin/mongod --logpath ~/railsprojects/md_alert/mdalert/source/log/mongodb.log --dbpath /mongodb/data/db
You can use your browser: http://localhost:28017/
Stopping
Ctrl-C doesn’t always want to truly kill it. Try
kill -2 pid
MongoDB Shell
It is a javascript-like interface…
Here is a good example
var today = ????? var purge = new Date(today.getTime() - 60 * 1000 * 24 * 3600); use mdalert-production; coll = db["patients"]; for( var c = db.message_logs.find().limit(10).sort({time : -1}); c.hasNext(); ) { print( c.next().time); }; use mdalert-production; clxn = "message_logs" date_key = "time" coll = db[clxn]; //var purge = new Date("12/09/2010"); var before_count = coll.count(); purge_count = coll.count({time: {$lt: purge}}); print("Purging " + purge_count + " records"); var t0 = new Date(); coll.remove({time: {$lt: purge}}) var t1 = new Date(); var after_count = coll.count(); var deleted_count = before_count - after_count; secs = (t1-t0)/1000 var rate = deleted_count/secs; print("Purged " + deleted_count + " records, at " + rate + " docs/sec") print("Reduced from " + before_count + " to " + after_count + " records")
Check out the stats:
> db.stats()
> db.patients.stats()
Common Commands
Getting help | In case you forget, handy references are built right in: |
help
Databases
show dbs
Set current DB
use mdalert-development
Collections
show collections
Setting a collection
db = db["message_logs"];
Looping
for( var c = db.find(); c.hasNext(); ) { print( c.next().time); }
create
> db.users.save({name : "Fred"}); > show collections system.indexes users > db.users.find() { "_id" : ObjectId("4da2690198737151210ba159"), "name" : "Fred" }
find
coll.find({time: {$lt: purge}})
remove
coll.remove({time: {$lt: purge}})
Examples…
Shell |
/mongodb/mongodb/bin/mongo MongoDB shell version: 1.2.2 url: test connecting to: test type "help" for help > db.person.save( { harry : 1 } ) > db.person.findOne() { "_id" : ObjectId("4b7616c738192c7c14c1b1ef"), "harry" : 1 } >db.patient.save( { id:1, last_name:"Dee",first_name:"Billy"}) >show dbs >use mdalert-development >show collections >db.users.find() >db.users.findOne({doctor_num:"670004"}) { "_id" : ObjectId("4ba02c748951a20463000006"), "notify_group" : false, "salutation" : "Dr.", "group_id" : ObjectId("4ba02c748951a20463000002"), "user_id" : ObjectId("4ba02c748951a20463000005"), "last_name" : "Shudderz", "notify_me" : true, "doctor_num" : "670004", "first_name" : "Suzanne" } |
Updating |
> s = db.settings.findOne({ identifier: 'ShowAllEvents'}); > s.value = "false"; > db.settings.save(s); |