码迷,mamicode.com
首页 > 其他好文 > 详细

bson的操作

时间:2015-08-17 15:31:19      阅读:311      评论:0      收藏:0      [点我收藏+]

标签:bson

bson的操作

flyfish 2015-8-27

使用的是bson-cpp

bson的创建

//{ name : ‘joe‘, age : 33.7 }
    //方式1
    bson::bo a = bson::bob().append("name", "joe").append("age", 33.7).obj();


    //方式2
    bson::bob x;
    x.append("name", "joe");
    x.append("age", 33.7);
    bson::bo b=x.obj();


    //方式3
    bson::bo c = BSON("name"<< "joe"
                    <<"age"<<33.7);

    //嵌套方式
     bson::bo d = BSON( "x" << "1" 
                << "y" << true
                << "subobj" << BSON( "m" << 3
                                  << "n" << 4 ) );

bson的输出

//输出方式1
    c["name"]
    c["age"]

    //输出方式2
    for( bson::bo::iterator it(c); it.more(); ) 
    {       
         string s=it.next().toString(); 
    }

    //输出方式3
    for(bson::bo::iterator it=c.begin(); it.more();)  
    {  
        bson::be t=it.next(); 

        string s=t.fieldName();// 输出的是 age,name

        if(t.type() == bson::String)
        {
            t.valuestr();//输出 joe
        }
        else if (t.type() == bson::NumberDouble)
        {
            t._numberDouble();//输出 33.7

        }

    }  

版权声明:本文为博主原创文章,未经博主允许不得转载。

bson的操作

标签:bson

原文地址:http://blog.csdn.net/flyfish1986/article/details/47725573

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!