MongoDB Quick Start

This learning note follows the tutorial MongoDB In 30 Minutes on Youtube.

Set up

  • Download and install
  • make a few folders where you want to store your database
  • and set the path to be used by mongodb:
    • mongod --directoryperdb --path 'directory' --logpath 'logpath/mongo.log' --logappend --rest --install installs the mongodb as a rest service
  • Now we can run the service: net start MongoDB(This is for windows only). the MongoDB now can run on the background as a service.
  • type in mongo and you are in the mongoDB shell.

Usual commands

  • show dbs: shows all the databases, don;t touch the default database local
  • use databasename: creates a new database
  • db shows you the current database you are in.
  • MongoDB use JSON format to interact with data. Can contain: string, number, array, objects, basically any normal data structure.
  • to create a user:

      db.createUser({
          user:"username",
          pwd:"password",
          roles:["roles of the user"]
      });
    
  • db.createCollection('customers') create a collection(table)
  • show collections will show all the collections
  • db.customers.insert({one json like entry}), note that ‘customer’

Everytime a new entry is inserted, an id is assigned.

  • db.customers.find()
  • db.customers.find().pretty()
  • db.customers.update({first_name:"John"},{first_name:"John", last_name:"...", }): replace an entry with a new entry, The ID does not change. The first parameter is used as a condition like WHERE.
  • How to update only one field(change/add/delete): Use operators : $set, $inc, $unset

    db.customers.update({condition},{$$$set:{gender:”male”}}) db.customers.update({condition},{$$$inc:{age:5}}) db.customers.update({first_name:”Steve”},{$$$unset:{age}})

  • db.customers.update({first_name:"NoExistName"},{first_name:"NoExistName", last_name:"K",},{upsert:true}); we can add a new entry if it is not find.
  • db.customers.update({condition},{$rename:{"gender":"sex"});: change attribute name
  • db.customer.remove({conditions},{justOne:true})remove the matching entries, if justOne is not specified, all matches will be deleted.
  • db.customers.find($or:[{condition one},{condition two}]) using logic operator.
  • $lt,$gt,$lte,$gte: db.customers.find({age:{$lt:40}}).pretty();
  • db.customers.find({"address.city":"Boston"}); : using attribute of attribute(member of dictionary, which is also a member of a dictionary). Note: dontforget the quots in "address.city".
  • Last one queried a Dictionary, we can also query an Array. For example, every/some entries have attribute membership=['mem1','mem2'] which is an array containing different kinds of memberships. we can do: db.customers.find({membership:"mem1"});
  • Sorting by some attribute: db.customer.find().sort({last_name:1}).pretty(). 1 for ascending, -1 for descending.
  • Count documents: db.customers.find({conditions}).count();
  • Set limit: db.customers.find({conditions}).limit(4).sort({last_name:1});
  • Iterate using forEach: db.customers.find().forEach(function(doc){print(doc.first_name)}); this could call javascript function.
Written on October 28, 2016