2018/05/04

MongoDB


SQL vs. NoSQL Solution
NoSQL data stores
  • Abandon SQL and ACID for simpler data and concurrency model
  • BASE semantics as opposed to ACID gaurantees
    • Basically Available
    • Sofe state - system state may change over time even without input
    • Eventually consistent
  • Key-value, document, column-family, graph


MongoDB
charateristics
A document is the basic uint of data
A collection can be thought of as a table with a dynamic schema
Single instance can host multiple independent databases
each of which can have its own colletions
comes with a Javascript shell
document is an ordered set of keys with associated values
type-sensitive and case-sensitive
documents cannot contain duplicate keys
key-value paris in documents are ordered

collections
is a group of documents
have dynamic schemas
a single collection have any number of different shapes

databases
groups collections into databases
can host several databases
each database is stored in separate files on disk
to store all data for a single application in the same database

shell
show dbs, use db_name, show collections
db.createCollection("collection_name")
db.collection_name.find()  
.findOne()
.find({"address.building":"386", "address.building":"381"})
==> 조건이 중복될 경우 마지막 381 조건에 만족하는 결과만 return
.find({$and:[{"address.street":"Canal Street"},{"address.building":"386"}]})
.find({$or:[{"address.building":"386"},{"address.building":"382"}]})
.find({"address.street":"Canal Street"},{"address.building":1, _id:0})
//0:column 제외, 1:조회 _id default 조회
.find({amount:{$lt:20, $gt:5}})

.insert({hello:'world'})
.insertOne({hello:'world'})
.insertMany({hello:'world'}, {good:'night'})

.update({title:'my blog'}, {title:'my blog name'})
.update({title:'my blog'}, {$set:{title:'my blog name'}})
.update({title:'my blog'}, {$unset:{title:''}})
attribute가 하나 였을 경우 record가 삭제됨
.update({title:'my blog'}, {$inc:{pageviews: 1}})
.update({title:'my blog'}, {$push:{name: "joe"}})
adds elements to the end of an array
.update({title:'my blog'}, {$push:{name: {$each:["bob", "kim", "lee"]}}})
use $addToSet to prevent duplicates
.update({title:'my blog'}, {$pop:{name: 1}})
removes it from the end, -1 from the begining
.update({title:'my blog'}, {$pull:{name: "lee"}})

.replaceOne({title:'my blog'},{header:'your blog'})

.remove()
if no parameter, remove all !
.remove({title:'my blog name'})
.deleteOne({title:'my blog name'})
.deleteMany({hello:'world'}, {good:'night'})

.drop()
deletes everyting, even the colletion

db.dropDatabase()

Data Types
Null : {"x" : null} 
Boolean : {"x" : true) 
Number : {"x" : 3.14}, {"x" : NumberLong(" 
0 
"foobar"} 
String : {"x" 
Date : {"x" : new Date()} 
• Objectld : {"x" : Objectld()} (a 12-byte ID f
  • Arrays can contain different data types
{"things":["pie", 3.14]}
  • embedded documents
"name" : "John Doe", 
"address" : { 
"123 Park 
"street" 
"city" 
"state" 
"Anytown't, 
t'NY'i
  • every documents stored in MongoDB must have an "_id" key
  • all documents must be smaller than 16MB