MongoDB笔记二 - C Driver 添加修改和查询

本文档介绍了如何使用C Driver进行MongoDB的增删改查操作,包括测试文件的组织结构和必要的头文件引用。
摘要由CSDN通过智能技术生成

这里有三个文件组成,一个是测试文件,另外两个是对MongoDB操作的封装

测试文件

#include "MongoDBWrapper.h"
#include "sperformance.h"

#include <iostream>
using namespace std;

#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string.hpp>

kagula::CMongoDBWrapper& mongoDB = kagula::CMongoDBWrapper::Instance();

//测试一天内插入多次。
//测试不同日期插入。
void testInsert()
{
	std::string cable_id("00d34f28-8bb9-4f22-a032-b46468d2176a");
	const int type_id = 2;
	std::string channel_id("c5e5ef1d-b31a-443b-a044-d70c64ac3354");
	int pos = 0;
	float fInterval = .0f;
	float value = .1f;

	mongoDB.insertOrUpdate(cable_id, type_id, channel_id, pos, fInterval, value);

	//100条记录,corei3-2120,用了23s+,虽然console output拖了后腿,这个更新速度也是无法接受的。
	//决定新建个方法,专门进行批处理更新。
	kagula::PerformanceTest pt;
	pt.start();
	for (int i = 0; i < 100;i++)
	{
	value += .01f;
	mongoDB.insertOrUpdate(cable_id, type_id, channel_id, pos, fInterval, value);
	}
	pt.stop();
	std::cout << "100 records update elapsed time:" << pt.toString() << std::endl << std::endl;
	
}

void testInsertBatch()
{
	std::string cable_id("00d34f28-8bb9-4f22-a032-b46468d2176a");
	const int type_id = 2;
	std::string channel_id("c5e5ef1d-b31a-443b-a044-d70c64ac3354");
	int pos = 0;
	float fInterval = .0f;
	float value = .2f;

	std::vector<double> vecInsert;
	for (int i = 0; i < 10 * 1000; i++)
	{
		vecInsert.push_back(i + value);
	}


	cout << "testInsertBatch 10k  test begin." << endl;
	kagula::PerformanceTest pt;
	pt.start();
	mongoDB.insertOrUpdate_batch(cable_id, type_id, channel_id, fInterval, vecInsert);
	pt.stop();
	//10k条记录,corei3-2120,新建记录用了1,760ms  - 3,655ms
	//10k条记录,corei3-2120,更新记录用了304,432ms
	cout << "testInsertBatch 10k  elapsed time:" << pt.toString() << endl;
}

void testSplit()
{
	string strBuffer;
	char buf[32] = { 0 };


	kagula::PerformanceTest pt;
	cout << "准备测试数据" << endl;
	for (int i = 0; i < 100*1000; i++)
	{
		sprintf_s(buf, "%d,", i);
		strBuffer += buf;
	}
	cout << "测试100k split性能!" << endl;

	pt.start();
	std::vector<std::string> vecRec;
	boost::split(vecRec, strBuffer, boost::is_any_of(","));
	pt.stop();
	//100k个点,corei3-2120  用了26,228ms+
	cout << "elapsed time:" << pt.toString();
}

void testLargeArrayCopy()
{
	std::vector<double> vecSrc,vecDest;


	cout << "void testLargeArrayCopy()  准备测试数据" << endl;
	unsigned nCount = 100 * 1000;
	for (int i = 0; i < nCount;i++)
	{
		vecSrc.push_back(i);
	}

	kagula::PerformanceTest pt;
	pt.start();
	vecDest.resize(vecSrc.size());
	for (int i = 0; i < nCount; i++)
	{
		vecDest[i] = vecSrc[i];
	}
	pt.stop();
	//100k个点,corei3-2120  用了29ms+
	cout << "elapsed time:" << pt.toString() << endl;
}

//测试查询指定时间位置的[cable+channel]
void testQuery()
{
	kagula::PerformanceTest pt;
	pt.start();

	std::string cable_id("00d34f28-8bb9-4f22-a032-b46468d2176a");
	std::string channel_id("c5e5ef1d-b31a-443b-a044-d70c64ac3354");
	int pos = 0;

	std::string outJSON;
	mongoDB.query(cable_id, channel_id, pos, outJSON);
	pt.stop();
	cout << "Query测试:" << pt.toString() << std::endl;
	cout << endl << "outJSON=>" << outJSON << endl;
}

int main(int argc, char *argv[])
{
	if (mongoDB.initDB() != 0)
	{
		cout << "Initialize mongoDB failed!" << endl;
		return -1;
	}

	//testSplit();
	//testLargeArrayCopy();

	//测试新建或更新document(记录)
	//testInsert();
	testInsertBatch();

	//测试返回查询结果
	//testQuery();

	cout << "input any key to close." << endl;
	cin.get();

	return 0;
}



用到的头文件

#ifndef _CMongoDBWrapper_H_
#define _CMongoDBWrapper_H_

#pragma once

#include <string>
#include <vector>

/*
Title: MongoDB Access
Author: kagula
Date: 2017-09-06
Environment:
[1]VS2013 Update5
[2]mongo-c-driver-1.7.0
*/
namespace kagula
{
	class CMongoDBWrapper
	{
	public:
		static CMongoDBWrapper& Instance
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kagula086

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值