After researching for a long time, I can finally connect to the mongodb server in windows vs2008 through C++ coding, so as to perform other database access operations.
1. The reason for spending so much time is that I didn’t understand these things at the beginning scons, python, SpiderMonkey,
The relationship between boost and mongodb, write down your personal understanding below.
To code the mongodb client under windows, you need to compile and generate a mongoclient.lib, which is the C++ interface class library required to connect to the mongodb server.
1. scons
scons is an automated build tool written in Python, which is similar to the make tool of linux. The associated SConstruct file is also a makefile file similar to the make tool,
Describes what and how to compile and link. Here is to use the scons tool to compile and generate mongoclient.lib (instead of vs).
2.python
Python is an object-oriented, literal computer programming language. Because scons is written in python, its library must be used, so install python before scons.
3. SpiderMonkey
A Javascript scripting engine implemented in C language, the data type format of mongodb is bson, and bson is the binary storage format of json,
json is the data type used by Javascript. mongodb supports Javascript scripting language for operation, so a Javascript scripting engine is needed
That is the SpiderMonkey.
4. boost
A very powerful C++ library, mongodb is written in C++, and this library is used, so it is needed.
2. Now let’s talk about how to generate mongoclient.lib under windows
1 Download and install python.
http://www.python.org/getit/, or you can download python-2.7.3.msi elsewhere
2Download and install scons. (Python is required, so install python first)
http://sourceforge.net/projects/scons/files/scons/2.2.0/,
And configure the python script path, add C:\Python27\Scripts to PATH.
3Download boost and put 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, decompress it in E:\mongodb-mongo-xxx\
http://dl.mongodb.org/dl/cxx-driver/
There is an SConstruct file in its main directory that will be used to compile mongoclient.lib with scons later,
5Download SpiderMonkey.
This link is https://github.com/dwight/vc2010_js programmed for vs2010,
However, I use vs2008. It is convenient to use it directly, and then I wrote some simple connections to the mongodb server and write and query operations can also be used. Compile a vs2008 version when you have time.
Create a directory js at the same level as mongodb to store downloaded files, such as E:\js\. Because r in that SConstruct file
The search path is ../js/
6 things are all downloaded and can be compiled.
The cmd command line enters E:\mongodb-mongo-xxx\,
Enter the command: scons mongoclient.lib
Wait a moment, if the compilation is successful, you can see the mongoclient.lib file in the E:\mongodb-mongo-xxx\ directory.
3. Write the client in vs2008
1 Create a new project and add code
#include “stdafx.h”
#include
#include “dbclient.h”
using namespace mongo;
void printIfAge(DBClientConnection& c, int age) {
auto_ptr 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”);
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 cursor = c.query(“tutorial.persons”, BSONObj());
while( cursor->more() ) {
cout <next().toString() << endl;
}
cout << "\nprintifage:\n";
printIfAge(c, 33);
}
int main() {
try {
run();
}
catch( DBException &e ) {
cout << "caught " << e.what() << endl;
}
return 0;
}
Include the boost directory and the client directory of mongodb’s C++ driver, such as: c:/boost;E:\mongodb-mongo-xxx\client
Also add this additional dependency library: ws2_32.lib mongoclient.lib (remember to add the path)
2 Open the mongodb server, the port is 30000
Then run the newly compiled client to see the connection.