MongoDB primary (compile MongoDB, C ++ to connect to MongoDB for testing, glib install)

Source: Internet
Author: User

C ++ language center
Click Open Link

C ++ driver download

Click Open Link

Install scons: CD build/sconspython setup. py install

Install PCRE and scons [root @:~ /Mongo-cxx-driver-v1.8] # scons after a period of formation, generate lib1_client. So: [root @:~ /Mongo-cxx-driver-v1.8] # lsauthtest clienttest firstexample lib+client. A license.txt sconstruct whereexampleclient config. Log httpclienttest lib+client. So Mongo secondexample

Copy to [root @: ~ in/usr/local/lib @:~ /Mongo-cxx-driver-v1.8] # cp lib1_client. So/usr/local/lib

Install boost Lib./Bootstrap. Sh./bjam install -- prefix =/usr

Glib install

glib install./configure --prefix=/usr make;make install gcc 'pkg-config --cflags --libs glib-2.0'gcc -_/usr/lib/glib-2.0/include/ -I/usr/include/glib-2.0 -lglib-2.0 XX.c

.................... Gorgeous demarcation line .........................

In addition, if you compile the MongoDB source code, you need to download the dependency package ftp://ftp.mozilla.org/pub/mozilla.org/js/js-1.7.0.tar.gzmake-F makefile. refjs_dist =/usr make-F makefile. ref export compile MongoDB and installtar-xvf mongodb-src-r1.4.4.tar.gzcd mongodb-src-r1.4.4scons -- Full Install also if you do not have a boost library, you also need to install boost |. /Bootstrap. sh->. /bjam install -- prefix =/usr/local after all the installations are completed, the corresponding MongoDB files will be available in/usr/loca include and libl.

................... Gorgeous demarcation line ..............

1. c ++ simple connection to MongoDB # 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;} Compilation: [root @:~ /SVN/MongoDB] # G ++ main. CPP-L/usr/local/lib/-I/usr/local/include/-l1_client-lboost_thread-lboost_filesystem-lboost_program_options run: [root @:~ /SVN/MongoDB] #./A. Out connected OK

2. mongoDB 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 ;}

# Include <iostream> # include "mongo/client/dbclient. H "using namespace Mongo; void printifage (dbclientconnection & C, int age) {auto_ptr <dbclientcursor> cursor = C. query ("tutorial. persons ", query (" Age "<age ). sort ("name"); While (cursor-> more () {bsonobj P = cursor-> next (); cout <p. getstringfield ("name") <Endl ;}void run () {dbclientconnection C; C. connect ("localhost"); cout <"connected OK" <Endl; bsonobj P = bson ("name" <"Joe" <"Age" <33); C. insert ("tutorial. persons ", P);/** <insert data to the person table */P = bson (" name "<" Jane "<" Age "<40 ); c. insert ("tutorial. persons ", P); P = bson (" name "<" Abe "<" Age "<33); C. insert ("tutorial. persons ", P ); P = bson ("name" <"Samantha" <"Age" <21 <"city" <"Los Angeles" <"state" <"ca "); c. insert ("tutorial. persons ", P); C. ensureindex ("tutorial. persons ", fromjson (" {age: 1} "); cout <" count: "<C. count ("tutorial. persons ") <Endl;/** <display the number of data in the person table */auto_ptr <dbclientcursor> cursor = C. query ("tutorial. persons ", bsonobj (); While (cursor-> more () {cout <cursor-> next (). tostring () <Endl ;}cout <"\ nprintifage: \ n"; printifage (C, 33) ;}int main () {try {run ();} catch (dbexception & E) {cout <"caught" <E. what () <Endl;} return 0 ;}
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.