사용 매뉴얼
MongoDB 문서 입력
이 예시에서는 Query Documents - MongoDB 페이지의 데이터를 동일하게 test
데이터베이스의 inventory
컬렉션에 입력하는 방법을 설명합니다. MongoDB 페이지에는 아래와 같은 MongoDB 쿼리가 있습니다:
await db.collection('inventory').insertMany([
{
item: 'journal',
qty: 25,
size: { h: 14, w: 21, uom: 'cm' },
status: 'A'
},
{
item: 'notebook',
qty: 50,
size: { h: 8.5, w: 11, uom: 'in' },
status: 'A'
},
{
item: 'paper',
qty: 100,
size: { h: 8.5, w: 11, uom: 'in' },
status: 'D'
},
{
item: 'planner',
qty: 75,
size: { h: 22.85, w: 30, uom: 'cm' },
status: 'D'
},
{
item: 'postcard',
qty: 45,
size: { h: 10, w: 15.25, uom: 'cm' },
status: 'A'
}
]);
로그프레소에서 MongoDB 앱을 이용하여 동일한 더미 데이터를 test
데이터베이스의 inventory
컬렉션에 입력하려면 아래와 같이 쿼리를 실행하세요:
json "{}"
| eval items = array(
dict("item", "journal", "qty", 25, "size", dict("h", 14, "w", 21, "uom", "cm"), "status", "A"),
dict("item", "notebook", "qty", 50, "size", dict("h", 8.5, "w", 11, "uom", "in"), "status", "A"),
dict("item", "paper", "qty", 100, "size", dict("h", 8.5, "w", 11, "uom", "in"), "status", "D"),
dict("item", "planner", "qty", 75, "size", dict("h", 22.85, "w", 30, "uom", "cm"), "status", "D"),
dict("item", "postcard", "qty", 45, "size", dict("h", 10, "w", 15.25, "uom", "cm"), "status", "A")
)
| explode items
| parsemap field=items
| mongodb-insert-batch profile="mongo" database="test" collection="inventory"
- array() 함수는 인자로 주어진 모든 객체를 하나의 리스트 객체로 생성합니다.
- dict() 함수는 키와 값을 연속적으로 입력받아 맵 객체를 생성합니다.
- explode 명령어는 지정된 필드의 값이 배열인 경우, 여러 개의 레코드로 분할합니다.
- parsemap 명령어는 지정된 필드의 값이 맵인 경우, 각 키-값을 필드로 변환합니다.
- mongodb-insert-batch 명령어는 입력 레코드로 전달된 모든 값을 지정된 MongoDB 컬렉션에 입력합니다.
MongoDB 문서 조회
이제 inventory
컬렉션에 입력된 모든 문서를 조회하려면 아래와 같이 쿼리를 실행하세요:
MongoDB 조건 검색
만약 status
값이 D
인 문서를 조회하려면 아래와 같이 쿼리를 실행하세요:
위와 같이 mongodb-docs 명령어의 filter
옵션에는 MongoDB 쿼리와 동일한 방식으로 필터 조건을 지정할 수 있습니다.
MongoDB 쿼리 연산자를 이용한 문서 검색
만약 status
값이 A
이거나 D
인 모든 문서를 검색하려면 아래와 같이 쿼리를 실행하세요:
mongodb-docs profile="mongo" database="test" collection="inventory"
filter="{ status: { $in: [ 'A', 'D' ] } }"
AND 조건을 이용한 문서 검색
inventory
컬렉션에서 status
값이 A
이고, qty
값이 30
보다 작은 경우를 검색하려면 아래와 같이 쿼리를 실행하세요:
mongodb-docs profile="mongo" database="test" collection="inventory"
filter="{ status: 'A', qty: { $lt: 30 } }"
OR 조건을 이용한 검색
inventory
컬렉션에서 status
값이 A
이거나 qty
값이 30
보다 작은 경우를 검색하려면 아래와 같이 쿼리를 실행하세요:
mongodb-docs profile="mongo" database="test" collection="inventory"
filter="{ $or: [{ status: 'A' }, { qty: { $lt: 30 } }] }"
MongoDB 문서 업데이트
inventory
컬렉션에서 item
값이 planner
인 문서의 status
필드를 E
로 업데이트하려면 아래와 같이 쿼리를 실행하세요:
json "{}"
| eval item="planner", status="E"
| mongodb-update-batch profile="mongo" database="test"
collection="inventory" keys="item" fields="status"
위의 mongodb-update-batch 명령어에서 사용된 옵션에 대한 설명은 아래와 같습니다:
profile
: 업데이트 대상 MongoDB 접속 프로파일 식별자database
: 업데이트 대상 MongoDB 데이터베이스 이름collection
: 업데이트 대상 MongoDB 컬렉션 이름keys
: 검색 기준 필드 이름. 여러 개의 키 필드를 기준으로 검색하려면 쉼표(,
)로 구분하여 입력하세요.fields
: 업데이트 대상 필드 이름. 여러 개의 필드를 업데이트하려면 쉼표(,
)로 구분하여 입력하세요.
MongoDB 문서 삭제
inventory
컬렉션에서 status
값이 A
인 모든 문서를 삭제하려면 아래와 같이 쿼리를 실행하세요:
json "{}"
| eval status="A"
| mongodb-delete-batch profile="mongo" database="test"
collection="inventory" keys="status"
만약 status
값이 A
인 문서 중에 첫번째 문서만 삭제하려면 아래와 같이 제한할 수 있습니다:
json "{}"
| eval status="A"
| mongodb-delete-batch profile="mongo" database="test"
collection="inventory" keys="status" limit=1
inventory
컬렉션의 모든 문서를 삭제하려면 아래와 같이 쿼리를 실행하세요: