mongodb 3 - mongodb 的c++程序访问

 

https://blog.csdn.net/qq_31253399/article/details/103389032

https://blog.csdn.net/qq_31253399/article/details/103503965

前面两篇文章介绍了前期的安装配置,虽然我们安装的是mongo-c-driver,但是因为c++对c的兼容性,我们仍可以进行操作

首先就是头文件

#include <bson.h>
#include <mongoc.h>

主函数

int main (int argc, char *argv[])

{
   mongoc_client_t *client;
   mongoc_collection_t *collection;
   mongoc_cursor_t *cursor;
   const bson_t *doc;
   bson_t *query;
   char *str;
   int  ID;
   char *key="_id";
   int num;

   client =mongoc_client_new ("mongodb://192.168.80.83:27017/?appname=find-example");

   collection = mongoc_client_get_collection (client, "mra7a", "model");

   mongoc_init ();

   //BSON_APPEND_UTF8 (query, "joint_type" , "test");
   KDL::Chain mra7a;
   KDL::Segment part;
   while(ID!=99)
   {
   std::cout<<"choose id:"<<std::endl;
   std::cin>>ID;
   if(ID==99)
       break;
   query = bson_new ();
   BSON_APPEND_INT64(query, key, ID);
   std::cout<<"**********begin**********"<<std::endl;
   cursor  = mongoc_collection_find (collection, MONGOC_QUERY_NONE, 0, 0, 0, query, NULL, NULL);
   while (mongoc_cursor_next (cursor, &doc)) {
      str = bson_as_json (doc, NULL);
      printf ("%s\n", str);
      part=GetRecord(doc);
      bson_free (str);
      std::cout<<"BACK MAIN"<<std::endl;

   }
   std::cout<<"**********end**********"<<std::endl;

}
   
   bson_destroy (query);

   mongoc_cursor_destroy (cursor);

   mongoc_collection_destroy (collection);

   mongoc_client_destroy (client);

   mongoc_cleanup ();

   show_segment_informention(mra7a);


   return 0;

}
我们使用mongoc_client_t声明一个client,之后连接局域网下的数据库
client =mongoc_client_new ("mongodb://192.168.80.83:27017/?appname=find-example");
之后输入模块的id作为索引取对应的模块数据
BSON_APPEND_INT64(query, key, ID);
把关键字key复制ID
cursor = mongoc_collection_find (collection, MONGOC_QUERY_NONE, 0, 0, 0, query, NULL, NULL);
用mongodb下的mongoc_collection_find找到相关的collection返回一个mongoc_cursor_t,之后通过 mongoc_cursor_next (cursor, &doc) 得到一个bson数据格式的doc,之后
str = bson_as_json (doc, NULL)
通过bson_as_json转换成json的字符串格式输出。但是字符串不是key-value形式的,无法直接使用,我们还是对bson进行操作
GetRecord(doc);

这个GetRecord(doc)

std::string joint_type,DH_type;
double DH_a_,DH_slpha_,DH_d_,DH_theta_;
double RigidBodyInertia_m,RigidBodyInertia_vector_x,RigidBodyInertia_vector_y,RigidBodyInertia_vector_z=.0;
double
RotationalInertia_Ixx,RotationalInertia_Iyy,RotationalInertia_Izz,RotationalInertia_Ixy,RotationalInertia_Iyz,RotationalInertia_Ixz=.0;
std::cout<<"into GetRecord"<<std::endl;
double number;
char *symbol;
bson_iter_t iter;
bson_iter_init(&iter, doc);
 
if (bson_iter_find(&iter, "joint_type")){
 bson_iter_find_value(&iter,"joint_type",number,symbol);
 joint_type=bson_iter_value(&iter)->value.v_utf8.str;
 }
通过collection中的key得到相对应的value,mongodb提供一种迭代寻找的方式
bson_iter_t iter;
bson_iter_init(&iter, doc);
通过bson_iter_init,将迭代器与doc相关联,之后通过迭代器find的函数找到关键字对应的迭代器
if (bson_iter_find(&iter,"joint_type")){
bson_iter_find_value(&iter,"joint_type",number,symbol);
joint_type=bson_iter_value(&iter)->value.v_utf8.str;
}
之后我们调用bson_iter_find_value去寻找值
注意monggdb中的迭代器遍历查询,有个缺点就是需要顺序遍历。比如abc的存储数据,我们查询acb只能得到ab,c找不到
void bson_iter_find_value(bson_iter_t *iter,char *key,double number,char *symbol)
{
 //std::cout << "key :"<<bson_iter_key(iter) <<std::endl;
 std::cout <<bson_iter_key(iter) <<":";

 if(bson_iter_value(iter)->value_type==0x02){
     //std::cout << "value:"<<bson_iter_value(iter)->value.v_utf8.str <<std::endl;
     std::cout <<bson_iter_value(iter)->value.v_utf8.str <<std::endl;
     symbol=bson_iter_value(iter)->value.v_utf8.str;
 }

}
迭代器中存储了一种type,不同的数据类型存的位置也不一样。因为这个是c语言的库,没有类的概念,所以就是根据数据类型将他存在结构体中的不同变量中。例
bson_iter_value(iter)->value_type==0x02
就是字符型,存在bson_iter_value(iter)->value.v_utf8.str中
我们从这个地方就可以通过key查到value

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值