一、什麼是MongoDBClient
MongoDBClient是MongoDB官方提供的C++17客戶端庫,用來連接、操作MongoDB資料庫。MongoDBClient提供了多種API,包括CRUD操作、聚合查詢、地理空間索引等。
二、如何使用MongoDBClient連接資料庫
使用MongoDBClient連接資料庫分為三個步驟:創建連接、指定資料庫、驗證身份。
#include
#include
int main() {
// 創建實例
mongocxx::instance inst{};
// 創建客戶端
mongocxx::client conn{ mongocxx::uri{} };
// 指定資料庫和集合
auto db = conn["dbname"];
auto coll = db["collectionname"];
// 憑據驗證
auto builder = bsoncxx::builder::stream::document{};
bsoncxx::document::value doc_value = builder
<< "user" << "myuser"
<< "pwd" << "mypwd"
<< bsoncxx::builder::stream::finalize;
auto creds = mongocxx::uri::auth_mechanism_properties{}
.add("SCRAM-SHA-1", bsoncxx::types::b_document{doc_value});
// 連接MongoDB
conn.authenticate(creds);
return 0;
}
三、CRUD操作
1. 插入數據
MongoDBClient提供了多種插入數據的API,最常用的是insert_one()和insert_many()。
// 插入單條數據
auto builder = bsoncxx::builder::stream::document{};
bsoncxx::document::value doc_value = builder
<< "name" << "Tom"
<< "age" << 20
<< bsoncxx::builder::stream::finalize;
coll.insert_one(doc_value.view());
// 插入多條數據
std::vector docs;
for (int i = 0; i < 5; ++i) {
auto builder = bsoncxx::builder::stream::document{};
bsoncxx::document::value doc_value = builder
<< "name" << "Tom"
<< "age" << 20+i
<< bsoncxx::builder::stream::finalize;
docs.push_back(doc_value);
}
coll.insert_many(docs);
2. 查詢數據
MongoDBClient提供了多種查詢數據的API,最常用的是find_one()和find()。
// 查詢單條數據
auto result = coll.find_one(
make_document(kvp("name", "Tom"))
);
std::cout << bsoncxx::to_json(*result) << std::endl;
// 查詢多條數據
auto cursor = coll.find(
make_document(kvp("age", make_document(kvp("$gte", 20))))
);
for (auto&& doc : cursor) {
std::cout << bsoncxx::to_json(doc) << std::endl;
}
3. 修改數據
MongoDBClient提供了多種修改數據的API,最常用的是update_one()和update_many()。
// 修改單條數據
coll.update_one(
make_document(kvp("name", "Tom")),
make_document(kvp("$set", make_document(kvp("age", 30))))
);
// 修改多條數據
coll.update_many(
make_document(kvp("name", "Tom")),
make_document(kvp("$set", make_document(kvp("age", 30))))
);
4. 刪除數據
MongoDBClient提供了多種刪除數據的API,最常用的是delete_one()和delete_many()。
// 刪除單條數據
coll.delete_one(
make_document(kvp("name", "Tom"))
);
// 刪除多條數據
coll.delete_many(
make_document(kvp("name", "Tom"))
);
四、聚合查詢
MongoDBClient支持多種聚合查詢操作,如計算總數、去重、分組、排序等。
// 計算總數
auto total_cursor = coll.aggregate(
make_array(bsoncxx::from_json(R"(
{ $count: "total" }
)"))
);
auto total_view = total_cursor.begin()->view();
std::cout << bsoncxx::to_json(total_view) << std::endl;
// 去重
auto distinct_cursor = coll.aggregate(
make_array(bsoncxx::from_json(R"(
{ $group: { _id: "$name" } },
{ $sort: { _id: 1 } }
)"))
);
for (auto&& doc : distinct_cursor) {
std::cout << bsoncxx::to_json(doc) << std::endl;
}
// 分組
auto group_cursor = coll.aggregate(
make_array(bsoncxx::from_json(R"(
{ $group: { _id: "$name", count: { $sum: 1 } } },
{ $sort: { count: -1 } }
)"))
);
for (auto&& doc : group_cursor) {
std::cout << bsoncxx::to_json(doc) << std::endl;
}
五、地理空間索引
MongoDBClient支持地理空間索引,可以實現根據經緯度查詢附近的位置。
// 創建地理空間索引
auto index_spec = bsoncxx::builder::stream::document{};
bsoncxx::document::value index_value = index_spec
<< "loc" << "2dsphere" << bsoncxx::builder::stream::finalize;
bsoncxx::document::view index_view = index_value.view();
coll.create_index(index_view);
// 插入數據
auto doc_builder = bsoncxx::builder::stream::document{};
bsoncxx::document::value doc_value = doc_builder
<< "name" << "Tom"
<< "loc" << bsoncxx::builder::stream::open_document
<< "type" << "Point"
<< "coordinates" << bsoncxx::builder::stream::open_array
<< 116.3975 << 39.9085
<< bsoncxx::builder::stream::close_array
<< bsoncxx::builder::stream::close_document
<< bsoncxx::builder::stream::finalize;
coll.insert_one(doc_value.view());
// 查找附近的位置
auto nearby_cursor = coll.find(
make_document(kvp("loc",
make_document(kvp("$nearSphere",
make_document(kvp("$geometry",
make_document(kvp("type", "Point"),
kvp("coordinates", make_array(116.4038, 39.915))))
),
kvp("$maxDistance", 5000)
))
))
);
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/187455.html
微信掃一掃
支付寶掃一掃