MongoDB C ++ started

Source: Internet
Author: User
Tags mongodb client

[From: http://blog.chinaunix.net/space.php? Uid = 7907749 & Do = Blog & id = 2037221]

 

I am preparing to send free time to MongoDB research recently, so there will be a series of articles to record this process. Download the Windows 32 version 1.2.1: Export -- dbpath E: \ lenkydatasource \ MongoDB \ 1.2.1 \ lenkytest \ dB -- Port 55555 from the official website and execute this file to start the MongoDB service program. The decompressed file also contains the C ++ header file and library file.
E: \ lenkydatasource \ MongoDB \ 1.2.1 \ mongodb-win32-i386-1.2.1 \ include
E: \ lenkydatasource \ MongoDB \ 1.2.1 \ mongodb-win32-i386-1.2.1 \ Lib use them to write the MongoDB client program:
First, check for the MongoDB library files provided in boostand db-win32-i386-1.2.1.zip.
E: \ lenkydatasource \ MongoDB \ 1.2.1 \ mongodb-win32-i386-1.2.1 \ Lib \ clients client. Lib
It is compiled using vs2008 + boost_000035_0, so you are too lazy to re-compile MongoDB. Therefore, you can use the corresponding vs2008 + boost_000035_0 compiling environment to download boost_000035_0 from the boost official website. Compile boost in two steps:
1. Run E: \ lenkydatasource \ MongoDB \ 1.2.1 \ boost_1_35_0 \ boost_1_35_0 \ tools \ jam \ build_dist.bat 2: copying data to E: \ lenkydatasource \ MongoDB \ 1.2.1 \ boost_1_35_0 \ release, then execute:
Cd e: \ lenkydatasource \ MongoDB \ 1.2.1 \ boost_1_35_0 \ boost_1_35_0
Bjam.exe link = static threading = multi variant = release runtime-link = static -- without-Python -- toolset = Many Lib libraries are generated after the msvc-9.0 is complete, such as the libboost_program_options-vc90-mt-gd-1_35.lib, copy all the Lib library files (search for the Lib keyword) to the E: \ lenkydatasource \ MongoDB \ 1.2.1 \ boost_1_35_0 \ boost_1_35_0 \ uselib directory (the uselib directory is created by myself) next, use vs2008 to create the project test, including the file first. CPP, and make the following settings:
1. Tools --> options --> projects and solutions --> VC ++ directory
Add the corresponding include file:
E: \ lenkydatasource \ MongoDB \ 1.2.1 \ boost_1_35_0 \ boost_1_35_0
E: \ lenkydatasource \ MongoDB \ 1.2.1 \ mongodb-win32-i386-1.2.1 \ include \ Mongo
Add the corresponding library file:
E: \ lenkydatasource \ MongoDB \ 1.2.1 \ boost_1_35_0 \ boost_1_35_0 \ uselib
E: \ lenkydatasource \ MongoDB \ 1.2.1 \ mongodb-win32-i386-1.2.1 \ lib2, project --> Properties (Alt + F7) --> Configure properties --> C/C ++ --> code generation --> Runtime Library
Select multiple threads (/mt), which is static. Do not use DLL. Compile and execute. Result:
Time: 10.797 s
Insert finished
Time: 0.453 s
Query finished
Time: 0.094 s
I have encountered many problems in the process of removing finished. After one solution, the correct process is like the above, and I do not know what is missing or not described. First. cpp file content:
// First. cpp # include <iostream> # include "client/dbclient. H" # pragma comment (Lib, "expose client. lib ")
# Pragma comment (Lib, "wsock32.lib") using namespace STD; int main (INT argc, const char ** argv ){
Int I;
Clock_t start, finish;
String errmsg;
String table = "Test. Test ";
Int record = 100000;
Mongo: dbclientconnection conn; // connect DB Server
If (! Conn. Connect (string ("127.0.0.1: 55555"), errmsg )){
Cout <"couldn't connect to server:" <errmsg <Endl;
Return-1;
}

// Insert Test Data
Start = clock ();
For (I = 0; I <record; I ++ ){
Mongo: bsonobjbuilder query;
Query <"user" <I <"PWD" <I <"Age" <I;
Query <"email" <I <"Address" <I <"phone" <I;
Conn. insert (table, query. OBJ ());
}
Finish = clock ();
Cout <"time:" <(double) (finish-Start)/clocks_per_sec <"S" <Endl;
Cout <"insert finished" <Endl; // query Test Data
Start = clock ();
{
Mongo: bsonobjbuilder query;
// Query. append ("user", 1 );
Auto_ptr <Mongo: dbclientcursor> cursor = conn. Query (table, query. OBJ ());
While (cursor-> more ()){
Mongo: bsonobj OBJ = cursor-> next ();
// Cout <obj. jsonstring () <Endl;
}
}
Finish = clock ();
Cout <"time:" <(double) (finish-Start)/clocks_per_sec <"S" <Endl;
Cout <"query finished" <Endl; // remove Test Data
Start = clock ();
{
Mongo: bsonobjbuilder query;
// Query. append ("user", 1 );
Conn. Remove (table, query. OBJ ());
}
Finish = clock ();
Cout <"time:" <(double) (finish-Start)/clocks_per_sec <"S" <Endl;
Cout <"Remove finished" <Endl; // waiting
Cin> errmsg;

Return 0;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.