MongoDB C ++ Development Environment setup

Source: Internet
Author: User
Tags install mongodb

MongoDB C ++ Development Environment setup

When I first came into contact with MongoDB C ++ development, I found that the C ++ driver source code package provided by the official team seemed to have some problems. After some hard work, I wrote down the first article here.
My Development Environment slackware 13.37x86 is used. Since the C ++ driver provided by the official team has some problems, we can build all MongoDB source code. A library, first from the (http://downloads.mongodb.org/src/mongodb-src-r1.8.2.tar.gz) to download the MongoDB source code package, compiled including the MongoDB itself and C ++ driver library.

Preparations before installation:

    • MongoDB depends on the JS library. You can install it on slackbuilds.org with sbopkg under slackware.
    • MongoDB depends on the boost library, which is included in the Development Tool library installed by slackware.
    • MongoDB uses the scons build tool. Therefore, you need to install scons. You can also install scons from slackbuilds.org under slackware.

Install MongoDB on $ home/usr/Mongo:

$ Mkdir-P ~ /Usr/Mongo

$ Tar xvf mongodb-src-r1.8.2.tar.gz

$ Scons -- prefix = $ home/usr/Mongo -- Full Install

After execution, check the three directories bin, include, and Lib under $ home/usr/Mongo.

Start MongoDB:

$ Mkdir-P ~ /Usr/mo_data

$ Cd ~ /Usr/Mongo/bin

$./Mongo -- dbpath = $ home/usr/mo_data

Write a simple littleProgramTest the libbench client. A generated.

 # Include  <  Iostream  >  
# Include " Client/dbclient. h "

Using Namespace Mongo;

Void Run (){
Dbclientconnection C;
C. Connect ( " Localhost " );
}

Int Main (){
Try {
Run ();
Cout < " Connected OK " < Endl;
} Catch (Dbexception & E ){
Cout < " Caught " < E. What () < Endl;
}
Return 0 ;
}

$ G ++ mon2.cpp-I ~ /Usr/Mongo/include/Mongo /~ /Usr/Mongo/lib/lib1_client. A-lboost_thread-lboost_filesystem-lboost_program_options

$./A. Out
Connected OK

The second program executes simple insert.

 # Include  <  Iostream  >  
# Include " 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 ++ mon3.cpp-I ~ /Usr/Mongo/include/Mongo /~ /Usr/Mongo/lib/lib1_client. A-lboost_thread-lboost_filesystem-lboost_program_options

$./A. Out

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.

(indicate the author and source when reprinting. Do not use it for commercial purposes without permission.)
more articles visit my blog: http://www.cnblogs.com/logicbaby

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.