๐ก ๋ณธ ๋ฌธ์๋ 'MongoDB C++ ํ๊ฒฝ ์ค์ ํ๊ธฐ'์ ๋ํด ์ ๋ฆฌํด๋์ ๊ธ์ ๋๋ค.
MongoDB ์๋ฒ์ ์ ์ํ์ฌ ๋ฐ์ดํฐ๋ฅผ ์ ์กํ๊ธฐ ์ํ ํด๋ผ์ด์ธํธ๋ฅผ ์ํ ํ๊ฒฝ์ ์ค์ ํ๋ ๋ฐฉ๋ฒ์ ๋ํด ์ ๋ฆฌํ์์ผ๋ ์ฐธ๊ณ ํ์๊ธฐ ๋ฐ๋๋๋ค.
1. mongocxx driver ์ค์น ์คํฌ๋ฆฝํธ
mongocxx driver๋ฅผ ์ค์นํ๊ธฐ ์ํ ์คํฌ๋ฆฝํธ ํ์ผ์ ๋๋ค. ์คํฌ๋ฆฝํธ์ ๋ํ ์์ธํ ์ค๋ช ์ ํ๋จ์ '2. mongocxx driver ์ค์น ์์ธ'์์ ์ ๋ฆฌํด๋์์ผ๋ ์ฐธ๊ณ ํ์๊ธฐ ๋ฐ๋๋๋ค.
#!/bin/bash
sudo apt-get install libmongoc-1.0-0
sudo apt-get install libbson-1.0-0
sudo apt-get install cmake libssl-dev libsasl2-dev
curl -OL https://github.com/mongodb/mongo-c-driver/releases/download/1.23.2/mongo-c-driver-1.23.2.tar.gz
tar xzf mongo-c-driver-1.23.2.tar.gz
cd mongo-c-driver-1.23.2
mkdir cmake-build
cd cmake-build
cmake .. \
-DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF \
-DCMAKE_INSTALL_PREFIX=/opt/mongo-c-driver \
-DCMAKE_PREFIX_PATH=/opt/mongo-c-driver
sudo cmake --build . --target install # ๋น๋์ ์ฑ๊ณตํ๋ฉด ์ค์น๊น์ง ์งํํ๋ค
cd ../../
curl -OL https://github.com/mongodb/mongo-cxx-driver/releases/download/r3.7.0/mongo-cxx-driver-r3.7.0.tar.gz
tar -xzf mongo-cxx-driver-r3.7.0.tar.gz
cd mongo-cxx-driver-r3.7.0/build
cmake .. \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_PREFIX_PATH=/opt/mongo-c-driver \
-DCMAKE_INSTALL_PREFIX=/opt/mongo-cxx-driver
sudo cmake --build . --target install
2. mongocxx driver ์ค์น ์์ธ
C++ API๋ฅผ ํ์ฉํ์ฌ mongoDB์ ์ ์ํ๊ธฐ ์ํด์๋ mongocxx driver๋ฅผ ์ค์นํ์ฌ์ผ ํฉ๋๋ค. ์์ธํ ์ค์น๊ณผ์ ์ ์์ผ๋ก ์ธ๊ธํ๊ฒ ์ง๋ง, ํ๋จ์ mongocxx driver๋ฅผ ์ค์นํ๊ณ ์ธํ ํ๋ ์คํฌ๋ฆฝํธ๋ฅผ ์ฒจ๋ถํ์์ผ๋ ์ฐธ๊ณ ํ์๊ธฐ ๋ฐ๋๋๋ค.
mongocxx driver๋ฅผ ์ค์นํ๊ธฐ ์์ mongocxx driver์์๋ 'libmongoc', 'libbson' ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ํ์ํ๊ธฐ์ ๋ค์์ ๋งํฌ๋ฅผ ํตํด mongoDB C Driver๋ฅผ ์ค์นํ ํ์ mongocxx driver๋ฅผ ์ค์นํ์ ์ผ ํฉ๋๋ค. ์ด๋ cmake ๋ช ๋ น์ด์ ์์ด ์ค์น ๊ฒฝ๋ก๋ฅผ /opt/mongo-c-driver๋ก ์ค์ ํด์ฃผ์๊ธฐ ๋ฐ๋๋๋ค.
% cmake .. \
-DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF \
-DCMAKE_INSTALL_PREFIX=/opt/mongo-c-driver \
-DCMAKE_PREFIX_PATH=/opt/mongo-c-driver
- [mongodb] Installing the MongoDB C Driver (libmongoc) and BSON library (libbson): http://mongoc.org/libmongoc/current/installing.html
'libmongoc', 'libbson' ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ค์นํ์๋ค๋ฉด mongocxx driver๋ฅผ ์ค์นํ์๋ฉด ๋ฉ๋๋ค. ์ด๋ cmake ๋ช ๋ น์ด์ ์์ด ์ค์น ๊ฒฝ๋ก๋ฅผ /opt/mongo-cxx-driver๋ก ์ค์ ํด์ฃผ์๊ธฐ ๋ฐ๋๋๋ค.
cmake .. \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_PREFIX_PATH=/opt/mongo-c-driver \
-DCMAKE_INSTALL_PREFIX=/opt/mongo-cxx-driverโ
- [mongodb] mongocxx driver install guide(linux): https://mongocxx.org/mongocxx-v3/installation/linux/
์ด์ mongoDB C++์ ์ฌ์ฉํ๊ธฐ ์ํ ํ๊ฒฝ ์ธํ ์ ๋ง์ณค์ต๋๋ค. ์ค์นํ ๊ฒ์ ๋ฐํ์ผ๋ก ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์ ์๋์ํ๋์ง ํ ์คํธ๋ฅผ ํด๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
3. mongoDB C++ API ํ ์คํธ
3.1 test.cpp
ํ ์คํธ๋ฅผ ์ํ .cpp ํ์ผ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
#include <cstdint>
#include <iostream>
#include <vector>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/stdx.hpp>
#include <mongocxx/uri.hpp>
#include <mongocxx/instance.hpp>
#include <bsoncxx/builder/stream/helpers.hpp>
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/builder/stream/array.hpp>
using bsoncxx::builder::stream::close_array;
using bsoncxx::builder::stream::close_document;
using bsoncxx::builder::stream::document;
using bsoncxx::builder::stream::finalize;
using bsoncxx::builder::stream::open_array;
using bsoncxx::builder::stream::open_document;
int main()
{
mongocxx::instance instance{}; // This should be done only once.
mongocxx::uri uri("mongodb://localhost:27017");
mongocxx::client client(uri);
mongocxx::database db = client["bsm"];
mongocxx::collection coll = db["test"];
auto builder = bsoncxx::builder::stream::document{};
bsoncxx::document::value doc_value = builder
<< "name"
<< "MongoDB"
<< "type"
<< "database"
<< "count" << 1
<< "versions" << bsoncxx::builder::stream::open_array
<< "v3.2"
<< "v3.0"
<< "v2.6"
<< close_array
<< "info" << bsoncxx::builder::stream::open_document
<< "x" << 203
<< "y" << 102
<< bsoncxx::builder::stream::close_document
<< bsoncxx::builder::stream::finalize;
bsoncxx::document::view view = doc_value.view();
}
3.2 test.cpp ์คํ
3.2.1 pkg-config ํ์ฉ
% c++ --std=c++11 <input>.cpp $(pkg-config --cflags --libs libmongocxx)
๋ง์ฝ์ ์๋๋ค๋ฉด PKG_CONFIG_PATH ํ๊ฒฝ๋ณ์์ libmongocxx.pc์ ์์น๋ฅผ ํฌํจ์ํต๋๋ค.
% nano ~/.bashrc
...
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/opt/mongo-cxx-driver/lib/pkgconfig
% source ~/.bashrc
3.2.2 ์ผ์ผํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๊ฒฝ๋ก ์ง์
pkg-config๊ฐ ์๋๋ค๋ฉด? include, library๋ฅผ ์ผ์ผํ ์ง์ ํ์ฌ ์คํํ ์ ์์ต๋๋ค.
% c++ --std=c++11 test.cpp -I/opt/mongo-cxx-driver/include/mongocxx/v_noabi -I/opt/mongo-cxx-driver/include/bsoncxx/v_noabi -I/opt/mongo-c-driver/include/libmongoc-1.0 -I/opt/mongo-c-driver/include/libbson-1.0 -L/opt/mongo-cxx-driver/lib -lmongocxx -lbsoncxx
3.2.3 CMake ๋น๋
mongodb๋ฅผ ๋น๋ํ ํ ๋์ค๋ shared ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๊ฐ์ง๊ณ CMake ํ์ผ์ ๋ง๋ค์ด ๋น๋ํ ์ ์์ต๋๋ค(ํ๋จ ๋งํฌ ์ฐธ์กฐ). ์ด๋ '# NOTE:'์ ์ ํ ๋ฐ์ ๊ฐ์ด 'CMAKE_PREFIX_PATH'๋ฅผ ์ด์ ์ ์ค์นํ์ ๋ ์ ์๋ ๊ฒฝ๋ก๋ก ์ค์ ํด์ฃผ์ด์ผ ํฉ๋๋ค.
set(CMAKE_PREFIX_PATH /opt/mongo-cxx-driver)
- [mongodb] CMakeLists(shared library): https://github.com/mongodb/mongo-cxx-driver/blob/master/examples/projects/mongocxx/cmake/shared/CMakeLists.txt
์ฐธ๊ณ
- [mongodb] MongoDB C++ Driver: https://www.mongodb.com/docs/drivers/cxx/#mongodb-compatibility
- [github] MongoDB C++ Driver: https://github.com/mongodb/mongo-cxx-driver
- [mongodb] Installing the mongocxx driver: https://mongocxx.org/mongocxx-v3/installation/
- [mongodb] mongocxx driver install guide(linux): https://mongocxx.org/mongocxx-v3/installation/linux/
- [mongodb] Installing the MongoDB C Driver (libmongoc) and BSON library (libbson): http://mongoc.org/libmongoc/current/installing.html
- [mongodb] mongocxx driver Advanced Configuration and Installation Options: https://mongocxx.org/mongocxx-v3/installation/advanced/
- [mongodb] Tutorial for mongocxx: https://mongocxx.org/mongocxx-v3/tutorial/
- [mongodb] CMakeLists(shared library): https://github.com/mongodb/mongo-cxx-driver/blob/master/examples/projects/mongocxx/cmake/shared/CMakeLists.txt
'Study: DeveloperTools(DevTool) > DevTool: NoSQL(Elastic, Mongo)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[NoSQL] MongoDB์ ์ ์ํ๊ธฐ (mongo-express, Robo 3T, VSCode) (0) | 2023.01.16 |
---|---|
[NoSQL] Docker๋ฅผ ์ด์ฉํ์ฌ MongoDB ์๋ฒ ๋์ฐ๊ธฐ (feat. mongo-express) (0) | 2023.01.10 |
[NoSQL] MongoDB Geospatial ์ฟผ๋ฆฌ: GeoJSON ๋ฐ์ดํฐ ํ์ฉํ๊ธฐ (0) | 2022.11.20 |
[NoSQL] Apache Cassandra ์ ๋ฆฌ: NoSQL Column-Family DB (0) | 2022.11.17 |
[NoSQL] Redis(vs Memcached) ์ ๋ฆฌ: NoSQL Key-value DB (0) | 2022.11.16 |