mongodb basic cmd code example

Example: 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)

Tags:

Go Example