代码在:https://github.com/xyz347/mongoxclient
受Golang启发写的,可以像golang一样直接操作结构体。只有基本接口,还不是很完备。
范例:(里面很多代码是用来初始化结构体的,和封装没关系,封装的接口后面加了注释方便区分)
#include <iostream>
#include <mongocxx/client.hpp>
#include <mongocxx/uri.hpp>
#include <mongocxx/instance.hpp>
#include "mongoxclient.hpp"
mongocxx::instance instance{};
using namespace std;
struct User {
int uid;
string name;
int age;
vector<string> tags;
vector<vector<int>> test;
XTOSTRUCT(A(uid, "bson:_id"), O(name, age, tags, test));
};
int main(int argc, char *argv[]) {
mongocxx::uri uri("mongodb://test:test@127.0.0.1:27018/test");
mongocxx::client client(uri);
mongocxx::collection collect = client["test"]["test"];
mongoxc::Collection col(collect); // 初始化一个Collection
col.RemoveAll({}); // 删除所有的document
User u1;
u1.uid = 123;
u1.name = "Hello";
u1.age = 20;
u1.tags.push_back("study");
u1.tags.push_back("play game");
u1.test.resize(2);
u1.test[0].push_back(1);
u1.test[0].push_back(2);
u1.test[0].push_back(3);
u1.test[1].push_back(4);
u1.test[1].push_back(5);
col.Upsert(u1.uid, u1); // 插入document
u1.uid = 456;
u1.name = "Good";
u1.age = 22;
u1.tags[0] = "windows";
u1.tags[1] = "linux";
u1.test.clear();
u1.test.resize(2);
u1.test[0].push_back(10);
u1.test[0].push_back(20);
u1.test[0].push_back(30);
u1.test[1].push_back(40);
u1.test[1].push_back(50);
col.Upsert(u1.uid, u1); // 插入document
u1.uid = 789;
u1.name = "insert";
col.Insert(u1); // insert struct
col.Insert(bb::vp{{"_id",1111}, {"name", "insert_bb"}}); // insert bb::vp
cout<<"======count=="<<endl;
cout<<col.Count()<<endl; // Count all
cout<<col.Find({{"_id",123}}).Count()<<endl; // Count query
cout<<"======get====="<<endl;
User get;
User getid;
vector<User> all;
col.Find({{"_id",123}}).One(get); // Find
cout<<x2struct::X::tojson(get)<<endl;
col.FindId(456).One(getid); // Find by ID
cout<<x2struct::X::tojson(getid)<<endl;
col.Find({}).All(all); // Find ALL
for (size_t i=0; i<all.size(); i++) {
cout<<x2struct::X::tojson(all[i])<<endl;
}
cout<<"===skip==="<<endl;
all.clear();
col.Find({}).Skip(1).All(all); // Skip
for (size_t i=0; i<all.size(); i++) {
cout<<x2struct::X::tojson(all[i])<<endl;
}
cout<<"===sort==="<<endl;
all.clear();
col.Find({}).Sort({{"_id",-1}}).All(all); // Sort
for (size_t i=0; i<all.size(); i++) {
cout<<x2struct::X::tojson(all[i])<<endl;
}
cout<<"===projection==="<<endl;
all.clear();
col.Find({}).Projection({{"name",1}}).All(all); // Projection
for (size_t i=0; i<all.size(); i++) {
cout<<x2struct::X::tojson(all[i])<<endl;
}
cout<<"===update all==="<<endl;
col.UpdateAll({}, {{"$set", bb::vp{{"name", "haha"}}}});
all.clear();
col.Find({}).All(all);
for (size_t i=0; i<all.size(); i++) {
cout<<x2struct::X::tojson(all[i])<<endl;
}
cout<<"=====remove====="<<endl;
col.Remove({{"_id",bb::vp{{"$lt", 124}}}});
cout<<col.Count()<<endl;
return 0;
}
运行结果:
======count==
4
1
======get=====
{"uid":123,"name":"Hello","age":20,"tags":["study","play game"],"test":[[1,2,3],[4,5]]}
{"uid":456,"name":"Good","age":22,"tags":["windows","linux"],"test":[[10,20,30],[40,50]]}
{"uid":123,"name":"Hello","age":20,"tags":["study","play game"],"test":[[1,2,3],[4,5]]}
{"uid":456,"name":"Good","age":22,"tags":["windows","linux"],"test":[[10,20,30],[40,50]]}
{"uid":789,"name":"insert","age":22,"tags":["windows","linux"],"test":[[10,20,30],[40,50]]}
{"uid":1111,"name":"insert_bb","age":22,"tags":[],"test":[]}
===skip===
{"uid":456,"name":"Good","age":22,"tags":["windows","linux"],"test":[[10,20,30],[40,50]]}
{"uid":789,"name":"insert","age":22,"tags":["windows","linux"],"test":[[10,20,30],[40,50]]}
{"uid":1111,"name":"insert_bb","age":22,"tags":[],"test":[]}
===sort===
{"uid":1111,"name":"insert_bb","age":2,"tags":[],"test":[]}
{"uid":789,"name":"insert","age":22,"tags":["windows","linux"],"test":[[10,20,30],[40,50]]}
{"uid":456,"name":"Good","age":22,"tags":["windows","linux"],"test":[[10,20,30],[40,50]]}
{"uid":123,"name":"Hello","age":20,"tags":["study","play game"],"test":[[1,2,3],[4,5]]}
===projection===
{"uid":123,"name":"Hello","age":2,"tags":[],"test":[]}
{"uid":456,"name":"Good","age":2,"tags":[],"test":[]}
{"uid":789,"name":"insert","age":2,"tags":[],"test":[]}
{"uid":1111,"name":"insert_bb","age":2,"tags":[],"test":[]}
===update all===
{"uid":123,"name":"haha","age":20,"tags":["study","play game"],"test":[[1,2,3],[4,5]]}
{"uid":456,"name":"haha","age":22,"tags":["windows","linux"],"test":[[10,20,30],[40,50]]}
{"uid":789,"name":"haha","age":22,"tags":["windows","linux"],"test":[[10,20,30],[40,50]]}
{"uid":1111,"name":"haha","age":22,"tags":[],"test":[]}
=====remove=====
3