A computer engineer poking at your cerebral cortex.

C++ Mongodb class (using BSON inserts)

I haven’t programmed in C++ in about 8 years so my skills may be a little rusty in my code so please suggest fixes where they need to be.

I needed to have a mongodb class to use and was inspired by this article http://tebros.com/2010/11/mongodb-bulk-inserts-with-the-c-driver/ to write this. I will eventually add username/password stuff but for now it work. It also reads in a single file in the same directory called env.txt so can switch environments easily.

/*
* Database.h -
* 1) Reads env.txt
* 2) checks connection
* 3) inserts bulk data thanks for this article: 
*     http://tebros.com/2010/11/mongodb-bulk-inserts-with-the-c-driver/
* 4) print count
* 5) retrieve all
*
*  Copyright 2010 Dan Sheffner.
*  You may use this work without restrictions, as long as this notice is included.
*  The work is provided "as is" without warranty of any kind, neither express nor implied.
*  More information can be found here: http://litl.info/
 */
 
#ifndef DATABASE_H_
#define DATABASE_H_
 
// Pre defined includes
#include <iostream>
#include <cstring>
#include <iostream>
#include <fstream>
#include <vector>
 
// Extra defined includes
#include "dbclient.h"
 
using namespace mongo;
using namespace std;
 
class Database
{
	char hostname[30];
	mongo::DBClientConnection mongo;
	std::vector<mongo::BSONObj> bulk_data;
	int recordCount;
	int insertThreshold;
 
 
public:
	Database();
	void readEnv();
	void connectMongodb();
	void testConnection();
	void dropCollection();
	void bulkDataInsert(string desc, BSONObj i);
	void bulkDisplay(string desc);
	void count(string desc);
	void insertRest(string desc);
};
 
Database::Database()
{
	recordCount = 1;
	insertThreshold = 10000;
}
void Database::connectMongodb()
{
	mongo.connect(hostname);
	//c.connect("localhost");
}
 
void Database::testConnection()
{
	try
	{
		connectMongodb();
		cout << "connected ok" << endl;
	} catch (DBException &e)
	{
		cout << "caught " << e.what() << endl;
	}
}
 
void Database::readEnv()
{
	ifstream myReadFile;
	myReadFile.open("env.txt");
	char output[100];
	if (myReadFile.is_open())
	{
		while (!myReadFile.eof())
		{
			myReadFile >> output;
		}
	}
	myReadFile.close();
 
	//two env
	if (strcmp(output, "dev") == 0)
	{
		cout << "the env is in dev mode" << endl;
		strcpy(hostname, "localhost");
	}
 
	if (strcmp(output, "prod") == 0)
	{
		cout << "the env is in prod mode" << endl;
	}
}
 
void Database::dropCollection()
{
	mongo.dropCollection("logs.ip");
}
 
void Database::bulkDataInsert(string desc, BSONObj i)
{
	bulk_data.push_back(i);
	//cout << recordCount << endl;
	recordCount = recordCount + 1;
 
	if (recordCount % insertThreshold == 0)
	{
		mongo.insert(desc, bulk_data);
		bulk_data.clear();
	}
}
 
void Database::insertRest(string desc)
{
	mongo.insert(desc, bulk_data);
	bulk_data.clear();
 
}
 
void Database::bulkDisplay(string desc)
{
	BSONObj emptyObj;
 
	auto_ptr<DBClientCursor> cursor = mongo.query(desc, emptyObj);
	while (cursor->more())
		cout << cursor->next().toString() << endl;
 
}
 
void Database::count(string desc)
{
	cout << "count of " << desc << ": " << mongo.count(desc) << endl;
}
 
#endif /* DATABASE_H_ */