728x90
반응형
food 샘플 데이터 입력
db.food.insertOne({_id:1, fruit: ["apple", "banana", "peach"]});
db.food.insertOne({_id:2, fruit: ["apple", "kumquat", "orange"]});
db.food.insertOne({_id:3, fruit: ["cherry", "banana", "apple"]});
db.food.insertOne({_id:4, fruit: ["cherry", "raspberry", "peach"]});
테스트
- 순서가 같아야 일치
MongoDB Enterprise > db.food.find({fruit: ["apple", "banana", "peach"]});
{ "_id" : 1, "fruit" : [ "apple", "banana", "peach" ] }
- 순서가 다르면 일치 하지 않는다.
MongoDB Enterprise > db.food.find({fruit: ["banana", "peach", "apple"]});
1. $all : 두개 동시에 가지고 있는 모든 도큐먼트를 찾는다.
MongoDB Enterprise > db.food.find({fruit: {$all : ["apple", "banana"]}});
{ "_id" : 1, "fruit" : [ "apple", "banana", "peach" ] }
{ "_id" : 3, "fruit" : [ "cherry", "banana", "apple" ] }
2. $in : 두개 중 하나라도 가지고 있는 도큐먼트를 찾는다.
MongoDB Enterprise > db.food.find({fruit: {$in : ["apple", "banana"]}});
{ "_id" : 1, "fruit" : [ "apple", "banana", "peach" ] }
{ "_id" : 2, "fruit" : [ "apple", "kumquat", "orange" ] }
{ "_id" : 3, "fruit" : [ "cherry", "banana", "apple" ] }
3. $size : 배열이 size값 인사람을 찾는다.
MongoDB Enterprise > db.food.find({fruit: {$size:3}});
{ "_id" : 1, "fruit" : [ "apple", "banana", "peach" ] }
{ "_id" : 2, "fruit" : [ "apple", "kumquat", "orange" ] }
{ "_id" : 3, "fruit" : [ "cherry", "banana", "apple" ] }
{ "_id" : 4, "fruit" : [ "cherry", "raspberry", "peach" ] }
MongoDB Enterprise > db.food.find({fruit: {$size:2}});
MongoDB Enterprise >
4. $push : 배열에 값을 추가 할 수 있다.
MongoDB Enterprise > db.food.updateOne({_id:4}, {$push: {fruit: "strawberry"}});
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
MongoDB Enterprise > db.food.findOne({_id:4});
{
"_id" : 4,
"fruit" : [
"cherry",
"raspberry",
"peach",
"strawberry"
]
}
5. $slice
- 0~2 (0번지 부터 2개)
MongoDB Enterprise > db.food.find({_id:4},{fruit:{$slice:2}});
{ "_id" : 4, "fruit" : [ "cherry", "raspberry" ] }
- 1~3 (1번지 부터 3개)
MongoDB Enterprise > db.food.find({_id:4},{fruit:{$slice: [1,3]}});
{ "_id" : 4, "fruit" : [ "raspberry", "peach", "strawberry" ] }
6. $pull : 배열속의 값 제거하기
MongoDB Enterprise > db.food.updateOne({_id:2}, {$pull : {fruit:"apple"}});
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 0 }
MongoDB Enterprise > db.food.find();
{ "_id" : 1, "fruit" : [ "apple", "banana", "peach" ] }
{ "_id" : 2, "fruit" : [ "kumquat", "orange" ] }
{ "_id" : 3, "fruit" : [ "cherry", "banana", "apple" ] }
{ "_id" : 4, "fruit" : [ "cherry", "raspberry", "peach", "strawberry" ] }
7. $elemMatch : 배열안에 document 여러 필드를 조건 걸어서 찾을 때 사용
- 데이터입력( post 생성 )
MongoDB Enterprise > db.post.insertOne({_id:1, replys : [
... {id:1, content:"내용1", userId: 1},
... {id:2, content:"내용2", userId: 1},
... {id:3, content:"내용3", userId: 2},
... ]});
{ "acknowledged" : true, "insertedId" : 1 }
MongoDB Enterprise > db.post.insertOne({_id:2, replys : [
... {id:4, content:"내용4", userId: 1},
... {id:5, content:"내용5", userId: 1},
... {id:6, content:"내용6", userId: 2},
... ]});
{ "acknowledged" : true, "insertedId" : 2 }
MongoDB Enterprise > db.post.find().pretty();
{
"_id" : 1,
"replys" : [
{
"id" : 1,
"content" : "내용1",
"userId" : 1
},
{
"id" : 2,
"content" : "내용2",
"userId" : 1
},
{
"id" : 3,
"content" : "내용3",
"userId" : 2
}
]
}
{
"_id" : 2,
"replys" : [
{
"id" : 4,
"content" : "내용4",
"userId" : 1
},
{
"id" : 5,
"content" : "내용5",
"userId" : 1
},
{
"id" : 6,
"content" : "내용6",
"userId" : 2
}
]
}
- post -> id:1, userId:1
MongoDB Enterprise > db.post.findOne({replys:{$elemMatch:{id:1,userId:1}}});
{
"_id" : 1,
"replys" : [
{
"id" : 1,
"content" : "내용1",
"userId" : 1
},
{
"id" : 2,
"content" : "내용2",
"userId" : 1
},
{
"id" : 3,
"content" : "내용3",
"userId" : 2
}
]
}
MongoDB Enterprise > db.post.findOne({replys:{$elemMatch:{id:1,userId:2}}});
null
- post -> id:1, userId:1 -> 1
> db.post.findOne({replys:{$elemMatch:{id:1,userId:1}}},{"replys.$":1});
{
"_id" : 1,
"replys" : [
{
"id" : 1,
"content" : "내용1",
"userId" : 1
}
]
}
8. 배열 내부의 전체 도큐먼트 갱신하기
- 하나씩 출력
> db.post.findOne({replys:{$elemMatch:{id:1,userId:1}}},{"replys.$":true});
{
"_id" : 1,
"replys" : [
{
"id" : 1,
"content" : "내용1",
"userId" : 1
}
]
}
> db.post.findOne({replys:{$elemMatch:{id:2,userId:1}}},{"replys.$":true});
{
"_id" : 1,
"replys" : [
{
"id" : 2,
"content" : "내용2",
"userId" : 1
}
]
}
> db.post.findOne({replys:{$elemMatch:{id:2,userId:1}}},{"_id":false,"replys.$":true});
{ "replys" : [ { "id" : 2, "content" : "내용2", "userId" : 1 } ] }
> db.post.findOne({replys:{$elemMatch:{id:1,userId:1}}},{"_id":false,"replys.$":true});
{ "replys" : [ { "id" : 1, "content" : "내용1", "userId" : 1 } ] }
- "_id" 없애서 출력 (_id:0 → false)
> db.post.find({_id:1},{replys:1,_id:0}).pretty();
{
"replys" : [
{
"id" : 1,
"content" : "내용1",
"userId" : 1
},
{
"id" : 2,
"content" : "내용2",
"userId" : 1
},
{
"id" : 3,
"content" : "내용3",
"userId" : 2
}
]
}
728x90
반응형
'DataBase > Mongo DB' 카테고리의 다른 글
Mongo DB 8강 - 샤딩 (0) | 2021.10.06 |
---|---|
Mongo DB 7강 - 주기마다 데이터 수집 및 DB 저장 (0) | 2021.10.01 |
Mongo DB 5강 - Update, Remove (0) | 2021.09.24 |
Mongo DB 4강 - Save, Find, 연산 (0) | 2021.09.23 |
Mongo DB 3강 - Spring Boot 연결 / CRUD (0) | 2021.09.23 |