NodeJS Mongo Basic

Contents


Install in Mac OS X

Install with homebrew

It’s easy way to install mongodb


Basic Operation using mongo

mongo is client application to manipulate

Start mongodb

mongod --dbpath ./data

You need to make data directory. data is data space.
By executing this command, mongod process starts

Connect database

mongo

mongo is client command

Show database list

show dbs

Switch database

use mydb

mydb is database name.

Check db object

db

Check db object to connect successfully

Show collection list

collection is like table of database
That is, data collection name

show collections

Save data

Insert data into collection

db.mydata.save({name:'yoona', mail:'yoona@snsd.com',memo:'this is test'})

Show data

basic

db.mydata.find()

{ “_id” : ObjectId(“54cc31d54a29d4a4ab3307b4”), “name” : “yoona”, “mail” : “yoona@snsd.com”, “memo” : “this is test” }

where clause

db.mydata.find({name:'yoona'})

Regular expression

db.mydata.find({name:/oo/})

Add condition

db.mydata.find({name:/oo/, mail:'yoona@snsd.com'})

MongoDB nodejs

mongodb : MongoDB driver
Mongoose : Modeling tool library

With Express

package.json
"dependencies":{
  "express": "3.2.6",
  "jade": "*",
  "mongoose": "3.x"
}

npm install(with Eclipse)
Point “package.json” -> Run As -> npm install

Before starting application, you need to start mongod program.

※ We need some steps to use mongoose in Mac OS X
This is great post about it.

Delete mongoose folder and npm install again

npm install