Mongo DB Collection 생성
> db.createCollection("test01")
{ "ok" : 1 }
db.test01.insert([
{
"name" : "천빈",
"sal": 1000,
"empno": 01
},
{
"name" : "승민",
"sal": 1100,
"empno": 02
},
{
"name" : "민정",
"sal": 1200,
"empno": 03
},
{
"name" : "원석",
"sal": 1300,
"empno": 04
}
])
find()를 document 확인하기
> db.test01.find().pretty() # select * from test01
{
"_id" : ObjectId("60c18778770fd4c895d73d80"),
"name" : "천빈",
"sal" : 1000,
"empno" : 1
}
{
"_id" : ObjectId("60c18778770fd4c895d73d81"),
"name" : "승민",
"sal" : 1100,
"empno" : 2
}
{
"_id" : ObjectId("60c18778770fd4c895d73d82"),
"name" : "민정",
"sal" : 1200,
"empno" : 3
}
{
"_id" : ObjectId("60c18778770fd4c895d73d83"),
"name" : "원석",
"sal" : 1300,
"empno" : 4
}
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!"
}
]
}
])
find() 조건 설정하기 - 특정 필드에 대한 값 가져오기
> db.articles.find({},{title:1, content:1})
## {} : 값을 가져올 빈방 , {title, content : 1 } : 이 컬렉션의 필드
{ "_id" : ObjectId("60c1837b770fd4c895d73d7c"), "title" : "article01", "content" : "content01" }
{ "_id" : ObjectId("60c1837b770fd4c895d73d7d"), "title" : "article02", "content" : "content02" }
{ "_id" : ObjectId("60c1837b770fd4c895d73d7e"), "title" : "article03", "content" : "content03" }
> db.articles.find({},{_id:0,title:1, content:1})
## _id : 0 -> 오브젝트 아이디를 나오지 않게
{ "title" : "article01", "content" : "content01" }
{ "title" : "article02", "content" : "content02" }
{ "title" : "article03", "content" : "content03" }
ObjectID
- 같은 Document 안에서 유일성이 보장되는 12byte binary data이다.
- [4byte : 시간정보][3byte : 머신별 고유 id][2byte : process ID][3byte : mongos가 생성한 inc]
- 이러한 조건으로 인해 각 오브젝트ID가 같을 경우의 수는 매우 낮다.
find() 조건 설정하기 - 특정 필드의 특정값을 가진 document 가져오기
> db.articles.find({"content" : "content03"}).pretty()
{
"_id" : ObjectId("60c1837b770fd4c895d73d7e"),
"title" : "article03",
"content" : "content03",
"writer" : "Bravo",
"likes" : 40,
"comments" : [
{
"name" : "Charlie",
"message" : "Hey Man!"
},
{
"name" : "Delta",
"message" : "Hey Man!"
}
]
}
select * from articles
where content = 'content03';
> db.articles.find({"content" : "content03"},{title :1,content : 1,_id:0}).pretty()
{ "title" : "article03", "content" : "content03" }
select title, content
from articles
where content = 'content03';
비교 연산자
- mongoDB역시 비교연산자를 통해 데이터를 조회할 수 있다.
- Query and Projection Operators — MongoDB Manual
$eq | equal |
$gt | greater than : 주어진 값보다 큰 |
$gte | greater than equal : 주어진 값보다 크거나 같은 |
$lt | less than : 주어진 값보다 작은 |
$lte | less than equal : 주어진 값보다 작거나 같은 |
$ne | not equal : 주어진 값과 같지않은 |
$in / $nin | 배열에 속하는 값 / 속하지 않는 값 |
find() 조건 설정하기 - 범위선언
> db.articles.find({"likes" : {$lte:30}}).pretty() ## Likes가 30보다 작거나 같은 데이터
{
"_id" : ObjectId("60c1aa2f4701a351ed3e9dc5"),
"title" : "article01",
"content" : "content01",
"writer" : "Velopert",
"likes" : 0,
"comments" : [ ]
}
{
"_id" : ObjectId("60c1aa2f4701a351ed3e9dc6"),
"title" : "article02",
"content" : "content02",
"writer" : "Alpha",
"likes" : 23,
"comments" : [
{
"name" : "Bravo",
"message" : "Hey Man!"
}
]
}
Save()
- insert, update와 같은 뜻을 가지지만 위의 두개는 특정 필드값을 수정, 추가하는 기능이지만 save()는 내용 전체 를 덮어쓰는 기능이다.
Save()를 통한 변화 확인하기
> db.exam03.save({item:"book",qty:40})
WriteResult({ "nInserted" : 1 })
> db.exam03.find()
{ "_id" : ObjectId("60c1a3de770fd4c895d73d87"), "item" : "book", "qty" : 40 }
> db.exam03.save({_id:1111,item:"book",qty:40})
WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : 1111 })
> db.exam03.find()
{ "_id" : ObjectId("60c1a3de770fd4c895d73d87"), "item" : "book", "qty" : 40 }
{ "_id" : 1111, "item" : "book", "qty" : 40 }
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.exam03.find()
{ "_id" : ObjectId("60c1a3de770fd4c895d73d87"), "item" : "book", "qty" : 40 }
{ "_id" : 1111, "item" : "book", "qty" : 140 }
> db.exam03.insert({_id:1111,item:"book",qty:140})
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 11000,
"errmsg" : "E11000 duplicate key error collection: my_test.exam03 index: _id_ dup key: { _id: 1111.0 }"
}
})
'IT > MongoDB' 카테고리의 다른 글
MongoDB - 기본개요 (0) | 2021.06.10 |
---|