IM/MDB/imm_mongodb/test_link.cpp

75 lines
2.6 KiB
C++

//
// Created by dongl on 23-5-24.
//
#include "template/MsgTemplate.h"
#include "mongocxx/instance.hpp"
#include "mongocxx/pool.hpp"
#include "mongocxx/uri.hpp"
#include "mongocxx/client.hpp"
#include "bsoncxx/json.hpp"
#include "MongoPool.h"
#include "bsoncxx/builder/stream/document.hpp"
using bsoncxx::builder::basic::kvp;
using bsoncxx::builder::basic::make_array;
using bsoncxx::builder::basic::make_document;
int main() {
mongocxx::instance instance{}; // This should be done only once.
mongocxx::uri uri("mongodb://user_session:Aa316216@124.221.152.192:27017/?authSource=im_session");
// 单链接
// 使用链接池
mongocxx::pool pool{uri};
// 自封转的一层连接池
auto client = pool.acquire();
// 选中库
auto db = client->database("im_session");
//
// auto doc_view = MsgTemplate::session_msg(mp::MP_SUB_TYPE::MP_IM_TEXT, mp::MP_SUB_TYPE::MP_FRIEND,
// 0, time(nullptr), 783556037, "测试消息");
//
// // 选中表
// auto collection = db.collection("chat");
// // 插入视图
// auto wr_ret = collection.insert_one(doc_view);
//
// //
// auto cursor_all = collection.find({});
// for (auto doc: cursor_all) {
// std::cout << to_string(doc["_id"].type()) << " " << doc["_id"].get_oid().value.to_string() << std::endl;
// std::cout << doc["count"].get_int32().value << std::endl;
//
// auto temp_view = doc["info"].get_document().value;
// std::cout << temp_view.find("x")->get_int32() << std::endl;
// std::cout << temp_view.find("y")->get_int32() << std::endl;
//
// std::cout << doc["name"].get_string().value << std::endl;
// std::cout << doc["type"].get_string().value << std::endl;
//
// auto version_arr = doc["versions"].get_array().value;
// for (auto mem = version_arr.begin(); mem != version_arr.end(); ++mem) {
// std::cout << mem->get_string().value << std::endl;
// }
// }
//
// std::cout << "collection " << collection.name()
// << " contains these documents:" << std::endl;
// for (auto doc : cursor_all) {
// std::cout << bsoncxx::to_json(doc, bsoncxx::ExtendedJsonMode::k_relaxed) << std::endl;
// }
//
// auto update_one_result =
// collection.update_one(make_document(kvp("name", "MongoDB")),
// make_document(kvp("$set", make_document(kvp("name", "update name")))));
//
//
// auto ret_filter = collection.find_one(make_document(kvp("count", 1)));
// if (ret_filter) {
// std::cout << ret_filter->find("_id")->get_oid().value.to_string() << std::endl;
// }
}