您的位置:首页 > 其它

bson的操作

2015-08-17 15:17 176 查看
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

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: