★ Mongo DB Manual ★
1. cls : 클리어 (cmd clear)
MongoDB Enterprise > cls
2. 데이터 저장
MongoDB Enterprise > db.users.save(user);
MongoDB Enterprise > db.users.save(user2);
MongoDB Enterprise > db.users.save(user3);
3. 컬렉션 확인
MongoDB Enterprise > show collections;
MongoDB Enterprise > user
4. 컬렉션 삭제 및 다시 추가
MongoDB Enterprise > db.users.drop();
true
MongoDB Enterprise > db.board.drop();
true
db.users.save({id:1, username:"ssar"});
db.users.save({id:2, username:"cos"});
db.users.save({id:3, username:"love"});
5. 전체 찾기
MongoDB Enterprise > db.users.find();
{ "_id" : ObjectId("614c219b60d3cb6c40faa181"), "username" : "ssar", "password" : "1234", "age" : 20 }
{ "_id" : ObjectId("614c21d960d3cb6c40faa182"), "username" : "cos", "password" : "1234", "age" : 25 }
{ "_id" : ObjectId("614c21e960d3cb6c40faa183"), "username" : "love", "password" : "1234", "age" : 30 }
6. 특정 데이터 찾기
- username 이 "cos"인 사람 찾기 : db.users.find({username:"cos"}).pretty();
- username 이 "cos"이면서 age가 "25"인 사람 찾기 : db.users.find({username:"cos",age:25}).pretty();
MongoDB Enterprise > db.users.find({username:"cos"}).pretty();
{
"_id" : ObjectId("614c21d960d3cb6c40faa182"),
"username" : "cos",
"password" : "1234",
"age" : 25
}
MongoDB Enterprise > db.users.find({username:"cos",age:25}).pretty();
{
"_id" : ObjectId("614c21d960d3cb6c40faa182"),
"username" : "cos",
"password" : "1234",
"age" : 25
}
7. 예쁘게 보기
- db.users.find().pretty();
MongoDB Enterprise > db.users.find();
{ "_id" : ObjectId("614c219b60d3cb6c40faa181"), "username" : "ssar", "password" : "1234", "age" : 20 }
{ "_id" : ObjectId("614c21d960d3cb6c40faa182"), "username" : "cos", "password" : "1234", "age" : 25 }
{ "_id" : ObjectId("614c21e960d3cb6c40faa183"), "username" : "love", "password" : "1234", "age" : 30 }
MongoDB Enterprise > db.users.find().pretty();
{
"_id" : ObjectId("614c219b60d3cb6c40faa181"),
"username" : "ssar",
"password" : "1234",
"age" : 20
}
{
"_id" : ObjectId("614c21d960d3cb6c40faa182"),
"username" : "cos",
"password" : "1234",
"age" : 25
}
{
"_id" : ObjectId("614c21e960d3cb6c40faa183"),
"username" : "love",
"password" : "1234",
"age" : 30
}
8. 특정 조건 찾기
- gt : Great Then
* 나이가 24보다 높은 사람 찾기
MongoDB Enterprise > db.users.find({age:{$gt:24}}).pretty();
{
"_id" : ObjectId("614c21d960d3cb6c40faa182"),
"username" : "cos",
"password" : "1234",
"age" : 25
}
{
"_id" : ObjectId("614c21e960d3cb6c40faa183"),
"username" : "love",
"password" : "1234",
"age" : 30
}
- lt : Less than
* 나이가 24보다 낮은 사람 찾기
MongoDB Enterprise > db.users.find({age:{$lt:24}}).pretty();
{
"_id" : ObjectId("614c219b60d3cb6c40faa181"),
"username" : "ssar",
"password" : "1234",
"age" : 20
}
- gte : Great Then Equal
* 나이가 25살 이상 인 사람 찾기
MongoDB Enterprise > db.users.find({age:{$gte:25}}).pretty();
{
"_id" : ObjectId("614c21d960d3cb6c40faa182"),
"username" : "cos",
"password" : "1234",
"age" : 25
}
{
"_id" : ObjectId("614c21e960d3cb6c40faa183"),
"username" : "love",
"password" : "1234",
"age" : 30
}
- lte : Less Than Equal
* 나이가 25살 이하 인 사람 찾기
MongoDB Enterprise > db.users.find({age:{$lte:25}}).pretty();
{
"_id" : ObjectId("614c219b60d3cb6c40faa181"),
"username" : "ssar",
"password" : "1234",
"age" : 20
}
{
"_id" : ObjectId("614c21d960d3cb6c40faa182"),
"username" : "cos",
"password" : "1234",
"age" : 25
}
9. 지정된 표현식 만들기 (let)
MongoDB Enterprise > let result = db.users.find({age:{$gte:25}});
MongoDB Enterprise > result;
{ "_id" : ObjectId("614c21d960d3cb6c40faa182"), "username" : "cos", "password" : "1234", "age" : 25 }
{ "_id" : ObjectId("614c21e960d3cb6c40faa183"), "username" : "love", "password" : "1234", "age" : 30 }
10. 연산
- IN(OR 여러개) 연산 : $in:[값1,값2] → 값1 OR 값2 하나의 필드만 가능
* age가 20 또는 25
MongoDB Enterprise > db.users.find({age:{$in:[20,25]}});
{ "_id" : ObjectId("614c219b60d3cb6c40faa181"), "username" : "ssar", "password" : "1234", "age" : 20 }
{ "_id" : ObjectId("614c21d960d3cb6c40faa182"), "username" : "cos", "password" : "1234", "age" : 25 }
- OR 연산 : 두 가지 이상의 필드 가능
* age가 20 또는 username 이 "ssar"
MongoDB Enterprise > db.users.find({$or:[{age:25},{username:"ssar"}]});
{ "_id" : ObjectId("614c219b60d3cb6c40faa181"), "username" : "ssar", "password" : "1234", "age" : 20 }
{ "_id" : ObjectId("614c21d960d3cb6c40faa182"), "username" : "cos", "password" : "1234", "age" : 25 }
- AND 연산
* 두가지 다 가능!
MongoDB Enterprise > db.users.find({username:"ssar",age:20}).pretty();
{
"_id" : ObjectId("614c219b60d3cb6c40faa181"),
"username" : "ssar",
"password" : "1234",
"age" : 20
}
MongoDB Enterprise > db.users.find({$and:[{"username":"ssar"},{"age":20}]}).pretty();
{
"_id" : ObjectId("614c219b60d3cb6c40faa181"),
"username" : "ssar",
"password" : "1234",
"age" : 20
}
- 객체 , 연결 연산
* bab 생성 + friend 추가
MongoDB Enterprise > db.users.save({username:"bab",password:"1234",age:35,friend:{username:"ssar",password:"1234",age:20}});
WriteResult({ "nInserted" : 1 })
* 결과값
MongoDB Enterprise > db.users.find({username:"bab"}).pretty();
{
"_id" : ObjectId("614c2b5260d3cb6c40faa184"),
"username" : "bab",
"password" : "1234",
"age" : 35,
"friend" : {
"username" : "ssar",
"password" : "1234",
"age" : 20
}
}
* 친구가 "ssar"인 사람 찾기
MongoDB Enterprise > db.users.find({"friend.username":"ssar"}).pretty();
{
"_id" : ObjectId("614c2b5260d3cb6c40faa184"),
"username" : "bab",
"password" : "1234",
"age" : 35,
"friend" : {
"username" : "ssar",
"password" : "1234",
"age" : 20
}
}
- NE(Negative)
* age: 25세가 아닌 사람 빼고 출력
MongoDB Enterprise > db.users.find({age:{$ne:25}}).pretty();
{
"_id" : ObjectId("614c219b60d3cb6c40faa181"),
"username" : "ssar",
"password" : "1234",
"age" : 20
}
{
"_id" : ObjectId("614c21e960d3cb6c40faa183"),
"username" : "love",
"password" : "1234",
"age" : 30
}
{
"_id" : ObjectId("614c2b5260d3cb6c40faa184"),
"username" : "bab",
"password" : "1234",
"age" : 35,
"friend" : {
"username" : "ssar",
"password" : "1234",
"age" : 20
}
}
- not : 부정의 부정 / 조건의 결과를 필드에 매칭시키기
MongoDB Enterprise > db.users.find({age:{$not : {$ne:25}}}).pretty();
{
"_id" : ObjectId("614c21d960d3cb6c40faa182"),
"username" : "cos",
"password" : "1234",
"age" : 25
}
- count : 전체 개수
MongoDB Enterprise > db.users.find().count();
4
- sort() 정렬
* 오름차순 (1)
MongoDB Enterprise > db.users.find().sort({age:1}).pretty();
{
"_id" : ObjectId("614c219b60d3cb6c40faa181"),
"username" : "ssar",
"password" : "1234",
"age" : 20
}
{
"_id" : ObjectId("614c21d960d3cb6c40faa182"),
"username" : "cos",
"password" : "1234",
"age" : 25
}
{
"_id" : ObjectId("614c21e960d3cb6c40faa183"),
"username" : "love",
"password" : "1234",
"age" : 30
}
{
"_id" : ObjectId("614c2b5260d3cb6c40faa184"),
"username" : "bab",
"password" : "1234",
"age" : 35,
"friend" : {
"username" : "ssar",
"password" : "1234",
"age" : 20
}
}
* 내림차순(-1)
MongoDB Enterprise > db.users.find().sort({age:-1}).pretty();
{
"_id" : ObjectId("614c2b5260d3cb6c40faa184"),
"username" : "bab",
"password" : "1234",
"age" : 35,
"friend" : {
"username" : "ssar",
"password" : "1234",
"age" : 20
}
}
{
"_id" : ObjectId("614c21e960d3cb6c40faa183"),
"username" : "love",
"password" : "1234",
"age" : 30
}
{
"_id" : ObjectId("614c21d960d3cb6c40faa182"),
"username" : "cos",
"password" : "1234",
"age" : 25
}
{
"_id" : ObjectId("614c219b60d3cb6c40faa181"),
"username" : "ssar",
"password" : "1234",
"age" : 20
}
- limit() : 제한해서 보여주기
* 두개만 보여주기 : limit(2)
MongoDB Enterprise > db.users.find().limit(2).pretty();
{
"_id" : ObjectId("614c219b60d3cb6c40faa181"),
"username" : "ssar",
"password" : "1234",
"age" : 20
}
{
"_id" : ObjectId("614c21d960d3cb6c40faa182"),
"username" : "cos",
"password" : "1234",
"age" : 25
}
* age 내림 차순, 두개만 보여주기
MongoDB Enterprise > db.users.find().sort({age:-1}).limit(2).pretty();
{
"_id" : ObjectId("614c2b5260d3cb6c40faa184"),
"username" : "bab",
"password" : "1234",
"age" : 35,
"friend" : {
"username" : "ssar",
"password" : "1234",
"age" : 20
}
}
{
"_id" : ObjectId("614c21e960d3cb6c40faa183"),
"username" : "love",
"password" : "1234",
"age" : 30
}
'DataBase > Mongo DB' 카테고리의 다른 글
Mongo DB 6강 - 배열 연산자 (고급) (0) | 2021.09.27 |
---|---|
Mongo DB 5강 - Update, Remove (0) | 2021.09.24 |
Mongo DB 3강 - Spring Boot 연결 / CRUD (0) | 2021.09.23 |
Mongo DB 2강 - RDBS vs NoSQL / 접속 (0) | 2021.09.23 |
Mongo DB 1강 - 설치 (0) | 2021.09.23 |