MongoDB C + + development environment build Linux Platform __ static function

Source: Internet
Author: User
Tags mongodb

First Contact MongoDB C + + development, found that the official supply of C + + driver source code package seems to have some problems, after some toss just smooth can run, so write down the first article.
I use the development environment Slackware 13.37 X86, since the official supply of C + + driver Some problems that all MongoDB source code to build. A library, first from (http://downloads.mongodb.org/src/ mongodb-src-r1.8.2.tar.gz) to download the MongoDB source package, compiled including the MongoDB itself and the C + + driver library.

Before installation: MongoDB relies on JS Library, Slackware can be installed from slackbuilds.org sbopkg. MongoDB relies on the boost library, which is already included in the Slackware installed development tool Library. MongoDB use scons build tools, you need to install Scons,slackware or install from slackbuilds.org

Install the MongoDB in the $home/usr/mongo:

$ mkdir-p ~/usr/mongo

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

$ scons--prefix= $HOME/usr/mongo--full Install

When you are done, view the three directories bin, include, Lib, $home/usr/mongo.

Start MongoDB:

$ mkdir-p ~/usr/mo_data

$ CD ~/usr/mongo/bin

$./mongo--dbpath= $HOME/usr/mo_data

Write a simple little program to test the generated libmongoclient.a.

#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/libmongoclient.a-lboost_thread-lboost_filesystem- Lboost_program_options

$./a.out
Connected OK

The second program performs a simple insert.

#include < iostream >
#include "Client/dbclient.h"

using namespace MONGO;

int main () {

Dbclientconnection Conn;
Bsonobj p = Bsonobjbuilder (). Append ("name", "Joe"). Append ("Age",). 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/libmongoclient.a-lboost_thread-lboost_filesystem- Lboost_program_options

$./a.out

Connected OK

Run MONGO client validation insert:

$ CD ~/usr/mongo/bin

$./mongo

MongoDB Shell version:1.8.2
Connecting To:test
> Show DBS
Admin (empty)
Local (empty)
Tutorial 0.0625GB
> 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 the use and development can be.

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.