MongoDB&C++开发 (六)使用bsoncxx::builder::stream和$push在数组中插入元素

遇到一个棘手的问题
我想构造的数据库如下

{
    _id:***
    videoName:***
    flag:***
    features: [
                    {
                        data:***
                        timestamp:***
                    }
                    {
                        data:***
                        timestamp:***
                    }
                    {
                        ……
                    }
              ]
}

我使用bsoncxx::builder::stream::document流式构造了整个文档,想在每次执行提取一帧特征的函数的时候将特征加入到features数组中,在网上没找到好的答案,基本上都是bsoncxx::builder::basic里面的处理,比较麻烦。
我的代码

videoCollection = collection;
videoDocument<<"videoName"<<fileList[videoPath]<<"flag"<<"advertisement";//videoDocument是bsoncxx::builder::stream::document类型
videoCollection.insert_one(videoDocument.view());
//一开始并不写入features数组

然后在我提取每帧特征的函数里

using bsoncxx::builder::stream::open_document;
using bsoncxx::builder::stream::close_document;
bsoncxx::builder::stream::document addDocBuilder;
bsoncxx::builder::stream::document singleFeature;
singleFeature<<"data"<<bufCDVS.data<<"timestamp"<<frame->pts;
addDocBuilder<<"$push"<<open_document<<"features"<<singleFeature<<close_document;
//使用$push指定features为所要添加元素的数组的名称,如果没有会新建一个。
//注意open_document和close_document是必不可少的的,且需要使用using
videoCollection.update_one(videoDocument.view(),addDocBuilder.view());
//使用update_one方法更新这个文档

最终的结果(使用robo3t做可视化工具)
Ubuntu screenshot挂了,补截图。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值