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.