Compile MongoDB and connect to MongoDB in C ++

Source: Internet
Author: User
1. c ++ driver download

Address: http://dl.mongodb.org/dl/cxx-driver/

Download the latest cxx-Driver/mongodb-linux-x86_64-v2.0-latest.tgz version here;

Note: Install PCRE and scons before compiling the driver. In addition, the G ++ version must be later than 4.0;

And need boost, about boost installation can refer to the previous article: http://blog.csdn.net/yuandianlws/article/details/7027538

2. Compile

[Root @ cdh3-0005 mongo-cxx-driver-v2.0] # scons

After compilation, libjavasclient. So will appear.

[Root @ cdh3-0005 mongo-cxx-driver-v2.0] # ls
Authtest clienttest firstexample lib1_client. A license.txt sconstruct whereexample
Client config. Log httpclienttest lib1_client. So Mongo secondexample

Copy libjavasclient. So to/usr/local/lib.

[Root @ cdh3-0005 mongo-cxx-driver-v2.0] # cp lib1_client. So/usr/local/lib

In addition, because mongo-cxx-driver does not have a 32-bit driver package, you can compile the MongoDB source code for installation, including the C ++ driver

However, it needs to download the dependency package
Ftp://ftp.mozilla.org/pub/mozilla.org/js
CD SRC
Make
Make install
Compile MongoDB and install
Tar-xvf mongodb-src-r2.0.4.tar.gz
CD mongodb-src-r2.0.4
Scons -- Full Install

3. Test

# Include <iostream>
# Include "mongo/client/dbclient. H"
Using namespace STD;
Using namespace Mongo;

Void run (){
Dbclientconnection C;
C. Connect ("localhost"); // Add port, C. Connect ("localhost: 27017 ")
}

Int main (void)
{
Try {
Run ();
Cout <"connected OK" <Endl;
} Catch (dbexception & E ){
Cout <"caught" <E. What () <Endl;
}
Return 0;

}
Compile:
[Root @:~ /SVN/MongoDB] # G ++ main. cpp-l1_client-lboost_thread-lboost_filesystem-lboost_program_options

Run:
[Root @:~ /SVN/MongoDB] #./A. Out
Connected OK

Another test:

# Include <iostream>
# Include "mongo/client/dbclient. H"

Using namespace Mongo;

Int main (){

Dbclientconnection conn;
Bsonobj P = bsonobjbuilder (). append ("name", "Joe"). append ("Age", 33). OBJ ();

Try {
Conn. Connect ("localhost ");
Cout <"connected OK" <Endl;
} Catch (dbexception & E ){
Cout <"caught" <E. What () <Endl;
}

Conn. insert ("tutorial. Persons", P );
Conn. insert ("tutorial. Persons", P );
Conn. insert ("tutorial. Persons", P );
Return 0;
}

G ++-O 1__insert into _insert.cpp-l1_client-lboost_thread-lboost_filesystem-lboost_program_options

$./Insert into connected OK

Run the Mongo client to verify insertion:

$ Cd ~ /Usr/Mongo/bin

$./Mongo

MongoDB shell version: 1.8.2
Connecting to: Test
> Show DBS
Admin (empty)
Local (empty)
Tutorial 0.0625 GB
> Use tutorial
Switched to DB tutorial
> DB. Persons. Find ()
{"_ Id": objectid ("4e11a582b918b66ebf3835fb"), "name": "Joe", "Age": 33}
{"_ Id": objectid ("4e11a582b918b66ebf3835fc"), "name": "Joe", "Age": 33}
{"_ Id": objectid ("4e11a582b918b66ebf3835fd"), "name": "Joe", "Age": 33}
>

OK, so that you can use and develop it.

Reference: MongoDB C ++ development environment to build a http://www.cnblogs.com/logicbaby/archive/2011/07/04/2097697.html

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.