Docs Menu
Docs Home
/ / /
C++ ๋“œ๋ผ์ด๋ฒ„
/

์ปค์„œ์—์„œ ๋ฐ์ดํ„ฐ ์•ก์„ธ์Šค

์ด ํŽ˜์ด์ง€์˜ ๋‚ด์šฉ

  • ๊ฐœ์š”
  • ๋ชจ๋“  ์ปค์„œ ๋ฌธ์„œ ์กฐํšŒ
  • ๋ฌธ์„œ ๊ฐœ๋ณ„ ์กฐํšŒ
  • ํ…Œ์ผ ์ปค์„œ(tailable cursor)
  • ์ถ”๊ฐ€ ์ •๋ณด

์ด ๊ฐ€์ด๋“œ ์—์„œ๋Š” C++ ์šด์ „์ž ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ปค์„œ ์—์„œ ๋ฐ์ดํ„ฐ์— ์•ก์„ธ์Šค ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ํ•™์Šต ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ปค์„œ ๋Š” ์ฝ๊ธฐ ์ž‘์—…์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•œ ๋ฐฐ์น˜๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์ปค๋‹ˆ์ฆ˜์ž…๋‹ˆ๋‹ค. ์ปค์„œ๋Š” ๋ชจ๋“  ๋ฌธ์„œ๋ฅผ ํ•œ ๋ฒˆ์— ๋ฐ˜ํ™˜ํ•˜๋Š” ๋Œ€์‹  ์ฃผ์–ด์ง„ ์‹œ๊ฐ„์— ๋ฌธ์„œ์˜ ํ•˜์œ„ ์ง‘ํ•ฉ๋งŒ ๋ณด์œ ํ•˜์—ฌ ๋ฉ”๋ชจ๋ฆฌ ์†Œ๋น„์™€ ๋„คํŠธ์›Œํฌ ๋Œ€์—ญํญ ์‚ฌ์šฉ๋Ÿ‰์„ ๋ชจ๋‘ ์ค„์ž…๋‹ˆ๋‹ค.

C++ ์šด์ „์ž ๊ฐ€ find() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฝ๊ธฐ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ๋•Œ๋งˆ๋‹ค mongocxx::cursor ์ธ์Šคํ„ด์Šค ์—์„œ ์ผ์น˜ํ•˜๋Š” ๋ฌธ์„œ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์ด ๊ฐ€์ด๋“œ ์˜ ์˜ˆ์ œ์—์„œ๋Š” Atlas ์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ ์„ธํŠธ ์˜ sample_restaurants ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์— ์žˆ๋Š” restaurants ์ปฌ๋ ‰์…˜ ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. C++ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์—์„œ ์ด ์ปฌ๋ ‰์…˜ ์— ์•ก์„ธ์Šค ํ•˜๋ ค๋ฉด Atlas cluster ์— ์—ฐ๊ฒฐํ•˜๋Š” mongocxx::client ๋ฅผ ์ธ์Šคํ„ด์Šคํ™”ํ•˜๊ณ  db ๋ฐ collection ๋ณ€์ˆ˜์— ๋‹ค์Œ ๊ฐ’์„ ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค.

auto db = client["sample_restaurants"];
auto collection = db["restaurants"];

๋ฌด๋ฃŒ MongoDB Atlas cluster ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ ์„ธํŠธ๋ฅผ ๋กœ๋“œํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ํ•™์Šต ๋ณด๋ ค๋ฉด Atlas ์‹œ์ž‘ํ•˜๊ธฐ ๊ฐ€์ด๋“œ ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

mongocxx::cursor ์ธ์Šคํ„ด์Šค ์˜ ๋‚ด์šฉ์„ ๋ฐ˜๋ณตํ•˜๋ ค๋ฉด for ๋ฃจํ”„๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ ์˜ˆ์‹œ ์—์„œ๋Š” find() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ name ๊ฐ’์ด "Dunkin' Donuts" ์ธ ๋ชจ๋“  ๋ฌธ์„œ๋ฅผ ์กฐํšŒ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ find() ๋ฉ”์„œ๋“œ์—์„œ ๋ฐ˜ํ™˜๋œ ์ปค์„œ ์—์„œ ๊ฐ ๋ฌธ์„œ ๋ฅผ ์ธ์‡„ํ•ฉ๋‹ˆ๋‹ค.

auto cursor = collection.find(make_document(kvp("name", "Dunkin' Donuts")));
for(auto&& doc : cursor) {
std::cout << bsoncxx::to_json(doc) << std::endl;
}
{ "_id" : { "$oid" : "..." }, ... "name" : "Dunkin' Donuts", "restaurant_id" : "40379573" }
{ "_id" : { "$oid" : "..." }, ... "name" : "Dunkin' Donuts", "restaurant_id" : "40363098" }
{ "_id" : { "$oid" : "..." }, ... "name" : "Dunkin' Donuts", "restaurant_id" : "40395071" }
...

์ปค์„œ ์—์„œ ๊ฐœ๋ณ„ ๋ฌธ์„œ ๋ฅผ ์กฐํšŒ ํ•˜๋ ค๋ฉด mongocxx::cursor ์ธ์Šคํ„ด์Šค ์—์„œ begin() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฉ”์„œ๋“œ๋Š” ์ปค์„œ ์˜ ์ฒซ ๋ฒˆ์งธ ๋ฌธ์„œ ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” mongocxx::cursor::iterator ์ธ์Šคํ„ด์Šค ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ ์˜ˆ์‹œ ์—์„œ๋Š” ์ปฌ๋ ‰์…˜ ์—์„œ name ๊ฐ’์ด "Dunkin' Donuts" ์ธ ๋ชจ๋“  ๋ฌธ์„œ๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ begin() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์ปค์„œ ์˜ ์ฒซ ๋ฒˆ์งธ ๋ฌธ์„œ ๋ฅผ ์ธ์‡„ํ•ฉ๋‹ˆ๋‹ค.

auto cursor = collection.find(make_document(kvp("name", "Dunkin' Donuts")));
auto doc = cursor.begin();
std::cout << bsoncxx::to_json(*doc) << std::endl;
{ "_id" : { "$oid" : "..." }, ... "name" : "Dunkin' Donuts", "restaurant_id" : "40379573" }

๊ณ ์ • ์‚ฌ์ด์ฆˆ ๊ณ ์ • ์‚ฌ์ด์ฆˆ ์ปฌ๋ ‰์…˜ ์„ ์ฟผ๋ฆฌํ•  ๋•Œ๋Š” ํด๋ผ์ด์–ธํŠธ ๊ฐ€ ์ปค์„œ ์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ชจ๋‘ ์‚ฌ์šฉํ•œ ํ›„์—๋„ ๊ณ„์† ์—ด๋ ค ์žˆ๋Š” ํ…Œ์ผ ์ปค์„œ( tailable cursor )๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ…Œ์ผ ์ปค์„œ( tailable cursor)๋ฅผ ๋งŒ๋“ค๋ ค๋ฉด mongocxx::options::find ๊ฐ์ฒด ๋ฅผ ์ธ์Šคํ„ด์Šคํ™”ํ•˜๊ณ  ํ•ด๋‹น cursor_type ํ•„๋“œ ๋ฅผ mongocxx::cursor::type::k_tailable ์œผ)๋กœ ์„ค์ •ํ•˜๋‹ค ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ mongocxx::options::find ์ธ์Šคํ„ด์Šค ๋ฅผ find() ๋ฉ”์„œ๋“œ์— ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ์˜ˆ์‹œ, ๋‹ค์Œ ์ฝ”๋“œ์™€ ๊ฐ™์ด ์•ผ์ฑ„๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฌธ์„œ๋ฅผ ์ €์žฅํ•˜๋Š” vegetables ์ด๋ผ๋Š” ๊ณ ์ • ์‚ฌ์ด์ฆˆ ์ปฌ๋ ‰์…˜ ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

auto db = client["db"];
auto collection = db.create_collection("vegetables", make_document(kvp("capped", true), kvp("size", 1024 * 1024)));
std::vector<bsoncxx::document::value> vegetables;
vegetables.push_back(make_document(kvp("name", "cauliflower")));
vegetables.push_back(make_document(kvp("name", "zucchini")));
auto result = collection.insert_many(vegetables);

๋‹ค์Œ ์ฝ”๋“œ์—์„œ๋Š” ํ…Œ์ผ ์ปค์„œ( tailable cursor )๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ vegetables ์ปฌ๋ ‰์…˜ ์˜ ๋ชจ๋“  ๋ฌธ์„œ๋ฅผ ์กฐํšŒ ํ•ฉ๋‹ˆ๋‹ค. ์ปค์„œ ๊ฐ€ ๋ชจ๋‘ ์†Œ์ง„๋˜๋ฉด ์„ธ ๊ฐœ์˜ ๋ฌธ์„œ๋ฅผ ๊ฒ€์ƒ‰ํ•  ๋•Œ๊นŒ์ง€ ์ปค์„œ๊ฐ€ ์—ด๋ฆฐ ์ƒํƒœ๋กœ ์œ ์ง€๋ฉ๋‹ˆ๋‹ค.

mongocxx::options::find opts{};
opts.cursor_type(mongocxx::cursor::type::k_tailable);
auto cursor = collection.find({}, opts);
int docs_found = 0;
while (docs_found < 3) {
for (auto&& doc : cursor) {
std::cout << bsoncxx::to_json(doc) << std::endl;
docs_found++;
}
// Sleeps for 100 milliseconds before trying to access more documents
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
{ "_id" : { "$oid" : "..." }, "name" : "cauliflower" }
{ "_id" : { "$oid" : "..." }, "name" : "zucchini" }

vegetables ์ปฌ๋ ‰์…˜ ์— ๋‹ค๋ฅธ ๋ฌธ์„œ ๋ฅผ ์‚ฝ์ž…ํ•˜๋Š” ๊ฒฝ์šฐ ์•ž์˜ ์ฝ”๋“œ๋Š” ์ƒˆ ๋ฌธ์„œ ๋ฅผ ์ธ์‡„ํ•˜๊ณ  while ๋ฃจํ”„๋ฅผ ๋‹ซ์Šต๋‹ˆ๋‹ค.

ํ…Œ์ผ ์ปค์„œ(tailable cursor)์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์•Œ์•„๋ณด๋ ค๋ฉด ๋งค๋‰ด์–ผ์˜ ํ…Œ์ผ ์ปค์„œ(tailable cursor) ๊ฐ€์ด๋“œ ๋ฅผ MongoDB Server ์ฐธ์กฐํ•˜์„ธ์š”.

์ฝ๊ธฐ ์ž‘์—…์— ํ•™์Šต ๋ณด๋ ค๋ฉด ๋ฐ์ดํ„ฐ ์กฐํšŒ ๊ฐ€์ด๋“œ ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

์ด ๊ฐ€์ด๋“œ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๋ฉ”์„œ๋“œ ๋˜๋Š” ์œ ํ˜•์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์•Œ์•„๋ณด๋ ค๋ฉด ๋‹ค์Œ API ์„ค๋ช…์„œ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

๋Œ์•„๊ฐ€๊ธฐ

๋ฌธ์„œ ์ˆ˜ ๊ณ„์‚ฐ