Mongodb c++ API的測試和封裝


安裝好c++的驅動后,對API進行了測試。官方api:https://mongodb.github.io/mongo-cxx-driver/api/legacy-1.0.0/

參考的代碼博文:https://www.cnblogs.com/edgarli/archive/2013/04/27/3046699.html

往數據庫中保存數據必須創建BSONObj類的對象,BSONObj下各組件都可以叫做BSONElement對象。使用BSONObjBuilder構造各種BSON對象,用BSONObjIterator來遍歷BSON對象。

 

BSONObj bsonEmpty = BSONObj();
BSONObj b = BSON("name"<<"xyj"<<"age"<<18);

insertDocument(&conn,collection,b);
//removeDocument(&conn,collection,b);

BSONObj data = updateData(&conn,collection,b);
string name = data.getStringField("name");
int age = data.getIntField("age");

BSONObj  resetAge = BSONObjBuilder().appendElements(b).append("sex","male").obj();

 

//查詢符合條件的元組

auto_ptr<DBClientCursor> cursor = conn.query( collection , BSONObj() );
int count = 0;
while (cursor->more())
{
count++;
BSONObj temp = cursor.next();
}

 

GridFS和GridFile類

一切都不如看源碼來的實在,API也湊合。

GridFile類:存儲於mongodb里的文件數據類,官方注釋: wrapper for a file stored in the Mongo database。用於獲取相關文件的內部數據,下載,判斷是否存在等

GridFS類:mongodb的文件操作類,官方注釋:GridFS is for storing large file-style objects in MongoDB.。用於GridFS數據庫的連接,文件的上傳、查找、刪除等

附上相關代碼:

string dbName = "PDFS";
string fileName = "D:\\PPT.pdf";
string remoteName = "up.pdf";
GridFS gf(conn,dbName);
gf.storeFile(fileName,remoteName);

GridFile gFile = gf.findFile(remoteName);
bool flag = gFile.exists();
gFile.write("D:\\down.pdf");

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM