MongoDB client Programming

Source: Internet
Author: User
Tags mongoclient mongodb client mongodb server

After studying it for a long time, we can connect to the MongoDB server through C ++ code in vs2008 of windows to access other databases.

I. The reason why I spent so much time is that I didn't know the relationship between scons, Python, spidermonkey, and boost and MongoDB at the beginning. I will write down my understanding below.
To encode the MongoDB client in Windows, you must compile and generate a mongoclient. Lib, that is, the C ++ Interface Class Library required to connect to the MongoDB server.
1. scons
Scons is an automated building tool written in Python, which has similar functions as the make tool in Linux. The associated sconstruct file is also the MAKEFILE file similar to the Make tool,
Describes the compilation and link content and methods. Here, the scons tool is used to compile and generate consumer client. Lib (instead of ).
2. Python
Python is an object-oriented, literal translation computer programming language. Because scons is written in Python, it is necessary to use its library, so install Python before scons.
3. spidermonkey
A Javascript script engine implemented in C language. The MongoDB Data Type format is bson, while bson is a JSON binary storage format,
JSON is the data type used by JavaScript. MongoDB supports operations in the Javascript script language, so a Javascript script engine is required.
This is the spidermonkey.
4. Boost
A very powerful C ++ library. MongoDB is written in C ++ and used in this library, so it is needed.

Ii. Now let's talk about how to generate client. Lib in windows.
1. Download and install python.
Http://www.python.org/getit/, or you can download python-2.7.3.msi from another location
2. Download and install scons. (python is required, so Python must be installed first)
Http://sourceforge.net/projects/scons/files/scons/2.2.0,
Configure the Python script path and add c: \ python27 \ scripts to the path.
3. Download boost and place it in c: \ boost.
Http://sourceforge.net/projects/boost/files/boost/1.52.0/boost_1_52_0.zip/download
4. Download the C ++ driver of MongoDB. For example, unzip the package to E: \ MongoDB-mongo-XXX \
Http://dl.mongodb.org/dl/cxx-driver/
There is an sconstruct file in its main directory, which will be used to compile javasclient. Lib with scons,
5. Download spidermonkey.
This link is a programmed https://github.com/dwight/vc2010_js for vs2010,
However, I used vs2008 to make it easy to use first, and then write some simple connections to the MongoDB server and write and query operations. Compile a vs2008 version later.
Create a directory similar to MongoDB where JS stores downloaded files, such as E: \ JS \. Because the r search path in the sconstruct file is./JS/
6 things are complete and can be compiled.
Run the CMD command to enter E: \ MongoDB-mongo-XXX \,
Enter the command: scons Consumer Client. Lib

Wait. If the compilation is successful, you can see the mongoclient. Lib file in the E: \ MongoDB-mongo-XXX \ directory.


3. Compile the client in vs2008

1. Create a project and add code

#include "stdafx.h"#include <iostream>#include "dbclient.h"// g++ tutorial.cpp -lmongoclient -lboost_thread -lboost_filesystem -o tutorialusing 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:30000"); //"192.168.58.1");cout << "connected ok" << endl;BSONObj p = BSON( "name" << "Joe" << "age" << 33 );c.insert("tutorial.persons", p);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;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;}

To include the boost directory and the MongoDB C ++ driver Client Directory, for example, C:/Boost; E: \ MongoDB-mongo-XXX \ Client

Add this additional dependent library ws2_32.lib using client. Lib (remember to add the path)
2. Open the MongoDB server. The port is 30000.
Run the compiled client and you will see the connection.
 

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.