use mongo db via cmd code example

Example 1: mongodb cmds

MongoDB

mongod --dbpath /users/yulin/Documents/data/db 
mongo (once mongod is ran OPEN ANOTHER TERMINAL THconnect to MongoDB)

ctrl-l to clear screen

show dbs
use {put db name here}
show collections
use {put collection name here} ex. -  use quizzes
use dbs ( or cd .. -- to go back dbs level)

or just stay @ db level 
db.createCollection(“quizzes”)
db.quizzes.find().pretty() 
db.quizzes.insert({“title”: 1}) (crud stuff)
db.quizzes.find({“title”: 1})
db.quizzes.remove({}) (remove all)
db.quizzes.remove({title: 1})
db.quizzes.update ({title: 1}, {title, 2}) (update by replaceing the entire object)
db.quizzes.update ({name: “yulin”}, {$set:{title: “Quiz 2”}})  (update by appending object)
db.quizzes.find({avg: {$gt: 85}}) (greater than)
db.quizzes.find({avg: {$gte: 85}}) (greater than or equal to)
db.quizzes.find({avg: {$lte: 85}}) (less than)

Example 2: open mongodb in cmd

1) Go to MongoDB bin folder:
		C:\Program Files\MongoDB\Server\3.2\bin
2) Enter command mongod
		By default, mongodb server will start at port 27017

Tags:

Misc Example