Getting started with Mongo

Installation:

  • brew update && brew doctor
  • brew install mongodb
    [justin@Justins-MacBook-Pro:~/Dev/node]$ brew install mongodb
    ==> Installing mongodb dependency: scons
    ==> Downloading https://downloads.sourceforge.net/scons/scons-2.3.4.tar.gz
    ######################################################################## 100.0%
    ==> /usr/bin/python setup.py install --prefix=/usr/local/Cellar/scons/2.3.4 --standalone
    🍺 /usr/local/Cellar/scons/2.3.4: 405 files, 4.4M, built in 16 seconds
    ==> Installing mongodb
    ==> Downloading http://downloads.mongodb.org/src/mongodb-src-r2.6.4.tar.gz
    ######################################################################## 100.0%
    ==> Patching
    patching file SConstruct
    patching file src/third_party/s2/util/endian/endian.h
    ==> /usr/local/opt/scons/bin/scons install --prefix=/usr/local/Cellar/mongodb/2.6.4_1 -j
    ==> Caveats
    To have launchd start mongodb at login:
    ln -sfv /usr/local/opt/mongodb/*.plist ~/Library/LaunchAgents
    Then to load mongodb now:
    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist
    Or, if you don't want/need launchctl, you can just run:
    mongod --config /usr/local/etc/mongod.conf
    ==> Summary
    🍺 /usr/local/Cellar/mongodb/2.6.4_1: 17 files, 331M, built in 11.6 minutes

Running the server:

  • mongod –config /usr/local/etc/mongod.conf (use CTRL-C to exit)

Using the client:

  • `mongo`
  • Syntax is JavaScript, can declare and use functions on the fly
  • Useful commands:
    • help
    • db – shows current database
    • show collections – display the collections in the current database
  • To show databases:
    > show dbs
  • To create (as needed), and use a database
    > use mydb
  • To show collections in the current database:
    > show collections
  • Example to create a JSON document
    > j = { name: “justin”, age: 41, status: “cool”, groups: [ “swimming”, “computer science” ] }
    > k = { name: “heather”, age: 39, status: “cool”, groups: [ “zumba” ] }
  • Example to create collection in current database (as needed), and insert a document:
    > db.testData.insert(j)
    > db.testData.insert(k)
  • Example to query all documents in a collection:
    > db.testData.find()
  • Example to query for a specific document in a collection (in this case, documents with age > 40) and sort:
    > db.testData.find({age: {$gt: 40} }).sort({age: 1})

References:

See also:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s