mongoDB 접속
$ mongosh mongodb://localhost:27017/admin?authSource=admin --username jamie
데이터 추가 MongoDB에 여러 데이터를 넣어보자(Insert & Import)
testDB 접근
$ mongosh mongodb://localhost:27017/testDB?authSource=admin --username jamie
Enter password: ***********
> use testDB
switched to db testDB
> db.inventory.find( { status: "D" } )
[
{
_id: ObjectId("5fc73f0993426845fee7ba6c"),
item: 'paper',
qty: 100,
size: { h: 8.5, w: 11, uom: 'in' },
status: 'D'
},
{
_id: ObjectId("5fc73f0993426845fee7ba6d"),
item: 'planner',
qty: 75,
size: { h: 22.85, w: 30, uom: 'cm' },
status: 'D'
}
]
> myCursor = db.inventory.find( { size: { h: 14, w: 21, uom: "cm" } } )
[
{
_id: ObjectId("5fc73f0993426845fee7ba69"),
item: 'journal',
qty: 25,
size: { h: 14, w: 21, uom: 'cm' },
status: 'A'
}
]
.을 이용하면 중첩 필드에도 조건을 걸 수 있다.
> myCursor = db.inventory.find( { "size.uom": "in" } )
[
{
_id: ObjectId("5fc73f0993426845fee7ba6b"),
item: 'notebook',
qty: 50,
size: { h: 8.5, w: 11, uom: 'in' },
status: 'A'
},
{
_id: ObjectId("5fc73f0993426845fee7ba6c"),
item: 'paper',
qty: 100,
size: { h: 8.5, w: 11, uom: 'in' },
status: 'D'
}
]
$ cat numbers.json
{"no" : 1}
{"no" : 2}
{"no" : 3}
{"no" : 4}
{"no" : 5}
{"no" : 6}
{"no" : 7}
{"no" : 8}
{"no" : 9}
{"no" : 10}
{"no" : 11}
{"no" : 12}
{"no" : 13}
{"no" : 14}
{"no" : 15}
{"no" : 16}
{"no" : 17}
{"no" : 18}
{"no" : 19}
{"no" : 20}
{"no" : 21}
{"no" : 22}
{"no" : 23}
{"no" : 24}
{"no" : 25}
{"no" : 26}
{"no" : 27}
{"no" : 28}
{"no" : 29}
{"no" : 30}
{"no" : 31}
{"no" : 32}
{"no" : 33}
{"no" : 34}
{"no" : 35}
{"no" : 36}
{"no" : 37}
{"no" : 38}
{"no" : 39}
{"no" : 40}
{"no" : 41}
$ mongoimport --db testDB --collection numbers --authenticationDatabase admin --username jamie --file ./inventory.json
Enter password:
2020-12-02T17:17:47.161+0900 connected to: mongodb://localhost/
2020-12-02T17:17:47.205+0900 41 document(s) imported successfully. 0 document(s) failed to import.
$ mongosh mongodb://localhost:27017/testDB?authSource=admin --username jamie
Enter password: ***********
# 20개만 조회함을 확인할 수 있음
> numberCursor = db.numbers.find({})
[
{ _id: ObjectId("5fc74dfa956cbf5449f7b66c"), no: 1 },
{ _id: ObjectId("5fc74dfa956cbf5449f7b66d"), no: 3 },
{ _id: ObjectId("5fc74dfa956cbf5449f7b66e"), no: 4 },
{ _id: ObjectId("5fc74dfa956cbf5449f7b66f"), no: 7 },
{ _id: ObjectId("5fc74dfa956cbf5449f7b670"), no: 6 },
{ _id: ObjectId("5fc74dfa956cbf5449f7b671"), no: 5 },
{ _id: ObjectId("5fc74dfa956cbf5449f7b672"), no: 8 },
{ _id: ObjectId("5fc74dfa956cbf5449f7b673"), no: 9 },
{ _id: ObjectId("5fc74dfa956cbf5449f7b674"), no: 10 },
{ _id: ObjectId("5fc74dfa956cbf5449f7b675"), no: 13 },
{ _id: ObjectId("5fc74dfa956cbf5449f7b676"), no: 2 },
{ _id: ObjectId("5fc74dfa956cbf5449f7b677"), no: 14 },
{ _id: ObjectId("5fc74dfa956cbf5449f7b678"), no: 16 },
{ _id: ObjectId("5fc74dfa956cbf5449f7b679"), no: 15 },
{ _id: ObjectId("5fc74dfa956cbf5449f7b67a"), no: 11 },
{ _id: ObjectId("5fc74dfa956cbf5449f7b67b"), no: 17 },
{ _id: ObjectId("5fc74dfa956cbf5449f7b67c"), no: 18 },
{ _id: ObjectId("5fc74dfa956cbf5449f7b67d"), no: 21 },
{ _id: ObjectId("5fc74dfa956cbf5449f7b67e"), no: 20 },
{ _id: ObjectId("5fc74dfa956cbf5449f7b67f"), no: 19 }
]
# 그렇다면 Cursor를 확인해보자 - 아까 조회한 값
> numberCursor
[
{ _id: ObjectId("5fc74dfa956cbf5449f7b66c"), no: 1 },
{ _id: ObjectId("5fc74dfa956cbf5449f7b66d"), no: 3 },
{ _id: ObjectId("5fc74dfa956cbf5449f7b66e"), no: 4 },
{ _id: ObjectId("5fc74dfa956cbf5449f7b66f"), no: 7 },
{ _id: ObjectId("5fc74dfa956cbf5449f7b670"), no: 6 },
{ _id: ObjectId("5fc74dfa956cbf5449f7b671"), no: 5 },
{ _id: ObjectId("5fc74dfa956cbf5449f7b672"), no: 8 },
{ _id: ObjectId("5fc74dfa956cbf5449f7b673"), no: 9 },
{ _id: ObjectId("5fc74dfa956cbf5449f7b674"), no: 10 },
{ _id: ObjectId("5fc74dfa956cbf5449f7b675"), no: 13 },
{ _id: ObjectId("5fc74dfa956cbf5449f7b676"), no: 2 },
{ _id: ObjectId("5fc74dfa956cbf5449f7b677"), no: 14 },
{ _id: ObjectId("5fc74dfa956cbf5449f7b678"), no: 16 },
{ _id: ObjectId("5fc74dfa956cbf5449f7b679"), no: 15 },
{ _id: ObjectId("5fc74dfa956cbf5449f7b67a"), no: 11 },
{ _id: ObjectId("5fc74dfa956cbf5449f7b67b"), no: 17 },
{ _id: ObjectId("5fc74dfa956cbf5449f7b67c"), no: 18 },
{ _id: ObjectId("5fc74dfa956cbf5449f7b67d"), no: 21 },
{ _id: ObjectId("5fc74dfa956cbf5449f7b67e"), no: 20 },
{ _id: ObjectId("5fc74dfa956cbf5449f7b67f"), no: 19 }
]
# 반복 돌리기
> while (numberCursor.hasNext()) {
... print(numberCursor.next());
...}
{ _id: ObjectId("5fc74dfa956cbf5449f7b680"), no: 22 }
{ _id: ObjectId("5fc74dfa956cbf5449f7b681"), no: 23 }
{ _id: ObjectId("5fc74dfa956cbf5449f7b682"), no: 24 }
{ _id: ObjectId("5fc74dfa956cbf5449f7b683"), no: 25 }
{ _id: ObjectId("5fc74dfa956cbf5449f7b684"), no: 26 }
{ _id: ObjectId("5fc74dfa956cbf5449f7b685"), no: 27 }
{ _id: ObjectId("5fc74dfa956cbf5449f7b686"), no: 28 }
{ _id: ObjectId("5fc74dfa956cbf5449f7b687"), no: 29 }
{ _id: ObjectId("5fc74dfa956cbf5449f7b688"), no: 31 }
{ _id: ObjectId("5fc74dfa956cbf5449f7b689"), no: 32 }
{ _id: ObjectId("5fc74dfa956cbf5449f7b68a"), no: 33 }
{ _id: ObjectId("5fc74dfa956cbf5449f7b68b"), no: 34 }
{ _id: ObjectId("5fc74dfa956cbf5449f7b68c"), no: 35 }
{ _id: ObjectId("5fc74dfa956cbf5449f7b68d"), no: 36 }
{ _id: ObjectId("5fc74dfa956cbf5449f7b68e"), no: 30 }
{ _id: ObjectId("5fc74dfa956cbf5449f7b68f"), no: 39 }
{ _id: ObjectId("5fc74dfa956cbf5449f7b690"), no: 37 }
{ _id: ObjectId("5fc74dfa956cbf5449f7b691"), no: 40 }
{ _id: ObjectId("5fc74dfa956cbf5449f7b692"), no: 41 }
{ _id: ObjectId("5fc74dfa956cbf5449f7b693"), no: 38 }
{ _id: ObjectId("5fc74dfa956cbf5449f7b694"), no: 12 }
# 재시도
> while (numberCursor.hasNext()) {
... print(numberCursor.next());
...}
MongoError: Cursor is closed
# numberCursor 재확인
> numberCursor
[
{ _id: ObjectId("5fc74dfa956cbf5449f7b66c"), no: 1 },
{ _id: ObjectId("5fc74dfa956cbf5449f7b66d"), no: 3 },
# 생략 - 동일 조회
]
MongoDB 공식 가이드 - https://docs.mongodb.com/guides/server/read_queries/