db.COLLECTION_NAME.find(query, projection)
parameter | Type | 설명 |
---|---|---|
query(filter) | document Optional | 다큐먼트를 조회할 때 기준 기준이 없이 컬렉션에 있는 모든 다큐먼트를 조회할 때는 이 매개변수를 비우거나 비어있는 다큐먼트 { } 를 전달 |
projection | document Optional | 다큐먼트를 조회할 때 보여질 또는 보여지지 않을 field 지정 |
Cursor 객체
> var myCur = db.inventory1.find()
> myCur
{ "_id" : ObjectId("61c3d12d3067e85d4239882b"), "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }
{ "_id" : ObjectId("61c3d12d3067e85d4239882c"), "item" : "notebook", "qty" : 50, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "A" }
{ "_id" : ObjectId("61c3d12d3067e85d4239882d"), "item" : "paper", "qty" : 100, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "D" }
{ "_id" : ObjectId("61c3d12d3067e85d4239882e"), "item" : "planner", "qty" : 75, "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "status" : "D" }
{ "_id" : ObjectId("61c3d12d3067e85d4239882f"), "item" : "postcard", "qty" : 45, "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "status" : "A" }
hasNext()
/ next()
forEach(func)
> db.inventory.insertMany([
{ item: "journal", qty: 25, tags: ["blank", "red"], dim_cm: [ 14, 21 ] },
{ item: "notebook", qty: 50, tags: ["red", "blank"], dim_cm: [ 14, 21 ] },
{ item: "paper", qty: 100, tags: ["red", "blank", "plain"], dim_cm: [ 14, 21 ] },
{ item: "planner", qty: 75, tags: ["blank", "red"], dim_cm: [ 22.85, 30 ] },
{ item: "postcard", qty: 45, tags: ["blue"], dim_cm: [ 10, 15.25 ] }
]);
> db.inventory.find( { tags: ["red", "blank"] } ) // 완벽히 일치해야 true
{ "_id" : ObjectId("61c3cbff3067e85d42398827"), "item" : "notebook", "qty" : 50, "tags" : [ "red", "blank" ], "dim_cm" : [ 14, 21 ] }
> db.inventory.find( { tags: { $all: ["red", "blank"] } } ) // 순서 상관없이 두개 다 포함만 하면 true
{ "_id" : ObjectId("61c3cbff3067e85d42398826"), "item" : "journal", "qty" : 25, "tags" : [ "blank", "red" ], "dim_cm" : [ 14, 21 ] }
{ "_id" : ObjectId("61c3cbff3067e85d42398827"), "item" : "notebook", "qty" : 50, "tags" : [ "red", "blank" ], "dim_cm" : [ 14, 21 ] }
{ "_id" : ObjectId("61c3cbff3067e85d42398828"), "item" : "paper", "qty" : 100, "tags" : [ "red", "blank", "plain" ], "dim_cm" : [ 14, 21 ] }
{ "_id" : ObjectId("61c3cbff3067e85d42398829"), "item" : "planner", "qty" : 75, "tags" : [ "blank", "red" ], "dim_cm" : [ 22.85, 30 ] }
> db.inventory.find( { dim_cm: { $elemMatch: { $gt: 22, $lt: 30 } } } ) // 값의 범위 지정
{ "_id" : ObjectId("61c3cbff3067e85d42398829"), "item" : "planner", "qty" : 75, "tags" : [ "blank", "red" ], "dim_cm" : [ 22.85, 30 ] }
> db.inventory.find( { "dim_cm.0": { $eq: 14 } } ) // dim_cm의 첫번째 값에 대한 비교
{ "_id" : ObjectId("61c3cbff3067e85d42398826"), "item" : "journal", "qty" : 25, "tags" : [ "blank", "red" ], "dim_cm" : [ 14, 21 ] }
{ "_id" : ObjectId("61c3cbff3067e85d42398827"), "item" : "notebook", "qty" : 50, "tags" : [ "red", "blank" ], "dim_cm" : [ 14, 21 ] }
{ "_id" : ObjectId("61c3cbff3067e85d42398828"), "item" : "paper", "qty" : 100, "tags" : [ "red", "blank", "plain" ], "dim_cm" : [ 14, 21 ] }
임베디드 : 객체 안에 객체가 중첩으로 있는 구조
> db.inventory1.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" }
]);
> db.inventory1.find( { size: { h: 14, w: 21, uom: "cm" } } , {_id : false}) // size의 h, w, uom이 정확하게 일치하는 도규먼트 검색
{ "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }
> db.inventory1.find( { "size.uom": "in" }, {_id : false} ) // .하위특정키 에 대한 값만 비교
{ "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" }
> db.inventory1.find( { "size.h": { $lt: 15 }, "size.uom": "in", status: "D" }, {_id : false} ) // 여러 조건을 함께(and) 비교
{ "item" : "paper", "qty" : 100, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "D" }
db.inventory2.insertMany( [
{ item: "journal", instock: [ { warehouse: "A", qty: 5 }, { warehouse: "C", qty: 15 } ] },
{ item: "notebook", instock: [ { warehouse: "C", qty: 5 } ] },
{ item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 15 } ] },
{ item: "planner", instock: [ { warehouse: "A", qty: 40 }, { warehouse: "B", qty: 5 } ] },
{ item: "postcard", instock: [ { warehouse: "B", qty: 15 }, { warehouse: "C", qty: 35 } ] }
]);
> db.inventory2.find( { "instock": { qty: 5, warehouse: "A" } }
// 순서가 달라 해당 객체를 못찾음 (instock이라는 객체로 비교를 하기 때문에 instock의 전체 값이 정확해야함)
> db.inventory2.find( { "instock": { $elemMatch: { qty: 5, warehouse: "A" } } } ) // qty가 5이고(and) warehouse가 A인 값 조회
{ "_id" : ObjectId("61c3d4de3067e85d42398830"), "item" : "journal", "instock" : [ { "warehouse" : "A", "qty" : 5 }, { "warehouse" : "C", "qty" : 15 } ] }
> db.inventory2.find( { "instock.qty": 5, "instock.warehouse": "A" } ) // qty가 5이거나(or) warehouse가 A인 값 조회
{ "_id" : ObjectId("61c3d4de3067e85d42398830"), "item" : "journal", "instock" : [ { "warehouse" : "A", "qty" : 5 }, { "warehouse" : "C", "qty" : 15 } ] }
{ "_id" : ObjectId("61c3d4de3067e85d42398833"), "item" : "planner", "instock" : [ { "warehouse" : "A", "qty" : 40 }, { "warehouse" : "B", "qty" : 5 } ] }
> db.inventory2.find( { 'instock.qty': { $lte: 5 } } ) // instock의 모든 qty에 대해
{ "_id" : ObjectId("61c3d4de3067e85d42398830"), "item" : "journal", "instock" : [ { "warehouse" : "A", "qty" : 5 }, { "warehouse" : "C", "qty" : 15 } ] }
{ "_id" : ObjectId("61c3d4de3067e85d42398831"), "item" : "notebook", "instock" : [ { "warehouse" : "C", "qty" : 5 } ] }
{ "_id" : ObjectId("61c3d4de3067e85d42398833"), "item" : "planner", "instock" : [ { "warehouse" : "A", "qty" : 40 }, { "warehouse" : "B", "qty" : 5 } ] }
> db.inventory2.find( { 'instock.0.qty': { $lte: 5 } } ) // instock의 첫번째 데이터의 qty에 대해
{ "_id" : ObjectId("61c3d4de3067e85d42398830"), "item" : "journal", "instock" : [ { "warehouse" : "A", "qty" : 5 }, { "warehouse" : "C", "qty" : 15 } ] }
{ "_id" : ObjectId("61c3d4de3067e85d42398831"), "item" : "notebook", "instock" : [ { "warehouse" : "C", "qty" : 5 } ] }
> db.inventory2.find( { "instock": { $elemMatch: { qty: { $gt: 10, $lte: 20 } } } } ) // 전체 qty 중 10초과 20이하인 값은 true
{ "_id" : ObjectId("61c3d4de3067e85d42398830"), "item" : "journal", "instock" : [ { "warehouse" : "A", "qty" : 5 }, { "warehouse" : "C", "qty" : 15 } ] }
{ "_id" : ObjectId("61c3d4de3067e85d42398832"), "item" : "paper", "instock" : [ { "warehouse" : "A", "qty" : 60 }, { "warehouse" : "B", "qty" : 15 } ] }
{ "_id" : ObjectId("61c3d4de3067e85d42398834"), "item" : "postcard", "instock" : [ { "warehouse" : "B", "qty" : 15 }, { "warehouse" : "C", "qty" : 35 } ] }
> db.inventory2.find( { "instock.qty": { $gt: 10, $lte: 20 } } ) // 전체 qty 중 10 초과는 true or 20 이하는 true
{ "_id" : ObjectId("61c3d4de3067e85d42398830"), "item" : "journal", "instock" : [ { "warehouse" : "A", "qty" : 5 }, { "warehouse" : "C", "qty" : 15 } ] }
{ "_id" : ObjectId("61c3d4de3067e85d42398832"), "item" : "paper", "instock" : [ { "warehouse" : "A", "qty" : 60 }, { "warehouse" : "B", "qty" : 15 } ] }
{ "_id" : ObjectId("61c3d4de3067e85d42398833"), "item" : "planner", "instock" : [ { "warehouse" : "A", "qty" : 40 }, { "warehouse" : "B", "qty" : 5 } ] }
{ "_id" : ObjectId("61c3d4de3067e85d42398834"), "item" : "postcard", "instock" : [ { "warehouse" : "B", "qty" : 15 }, { "warehouse" : "C", "qty" : 35 } ] }