서버 접속
> mongo
DB
생성: use
> use mongodb_sample
> db
> show dbs

- 최소 한 개의
Document를 추가해야 DB 리스트에서 확인 가능
제거: db.dropDatabase()
> use mongodb_sample
> db.dropDatabase();
Collection
생성: db.createCollection()
option

> use test
> db.createCollection("books")
> db.createCollection("articles", {
... capped: true,
... autoIndex: true,
... size: 6142800,
... max: 10000
... })
> db.people.insert({"name": "velopert"})
> show collections
제거: db.[collection 이름].drop()
> use test
> db.people.drop()
Document
추가: db.[collection 이름].insert(document)
> use books
> db.books.insert({"name": "NodeJS Guide", "author": "Velopert"})
> db.books.insert([
... {"name": "Book1", "author": "Velopert"},
... {"name": "Book2", "author": "Velopert"}
... ]);
> db.books.find()
제거: db.[collection 이름].remove(criteria, justOne)
매개변수

> db.books.find({"name": "Book1"})
> db.books.remove({"name": "Book1"})
> db.books.find()
조회: db.[collection 이름].find(query, projection)
매개변수

반환값: cursor
- cursor를 사용하여 데이터 sort 가능
- 10분 동안 사용되지 않으면 만료됨
> db.articles.insert([
{
"title" : "article01",
"content" : "content01",
"writer" : "Velopert",
"likes" : 0,
"comments" : [ ]
},
{
"title" : "article02",
"content" : "content02",
"writer" : "Alpha",
"likes" : 23,
"comments" : [
{
"name" : "Bravo",
"message" : "Hey Man!"
}
]
},
{
"title" : "article03",
"content" : "content03",
"writer" : "Bravo",
"likes" : 40,
"comments" : [
{
"name" : "Charlie",
"message" : "Hey Man!"
},
{
"name" : "Delta",
"message" : "Hey Man!"
}
]
}
])
> db.articles.find*(
> db.articles.find().pretty()
> db.articles.find({"writer": "Velopert"}).pretty()
> db.articles.find({"likes": {$lte: 30}}).pretty()
> db.articles.find( { } , { "_id": false, "title": true, "content": true } )
MongoDB Query 연산자
$eq: equals
$ne: not equals
$gt: greater then
$gte: greater then or equals
$lt: less then
$lte: less then or equals
$in, $nin
- 논리 연산자:
$or, $and, $not, $nor
- regex 연산자
- where 연산자
- slice 연산자
- elemMatch 연산자