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. library a, first download the MongoDB source code package from the (http://downloads.mongodb.org/src/mongodb-src-r1.8.2.tar.gz), after the compilation includes 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 applet to test the generated lib1_client..
#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.