Last Updated: September 09, 2019
·
1.284K
· kristinhenry

Handy Notes on Using MongoDB

Until recently, most of my database programming experience has been with mySQL. But I've been using MongoDB, more and more. Here are some notes I keep on hand, especially when I haven't used MongoDB for a while and need a quick refresh.

Notes on MongoDB/Steps to run the program:

  1. from command prompt: start MongoDB (local installation)

a. navigate to directory for Mongo on this system:

C:\mongo\bin

b. start the db

C:\mongo\bin> mongod

  1. To interact with the database, start a new command prompt (keeping the first open)

a. navigate to directory for Mongo on this system:

C:\mongo\bin

b. start session

C:\mongo\bin\ >mongo

c. some useful commands:

show dbs
use db_name

show collections // collections are like tables in SQL

db.collection_name.find() // shows all objects in that collection

// add data

obj = { name : "Kathy" };
db.collection_name.save(obj);

// get data

db.collection_name.find({x:4})

// delete data

db.collection_name.drop() // drop the whole collection

db.collectionname.remove() // remove all object from collection
db.collection
name.remove({ name: “Kathy”}) // remove kathy

I originally posted this on my blog, at http://www.underground-flash.com/2012/07/handy-notes-on-using-mongodb.html