Mongodb C++ 正则表达式查询

Sam*_*ali 5 c++ regex mongodb mongo-cxx-driver

如何在 C++ 中使用正则表达式查询 MongoDB 数据库。

mongo-cxx-driver-r3.1.1

听到包括

#include <cstdlib>
#include <iostream>
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>
#include <cstdint>
#include <vector>
#include <mongocxx/stdx.hpp>
#include <bson.h>
#include <conio.h> 
#include <sstream> 
#include <stdio.h> 
#include <string>
#include <bsoncxx/types.hpp>
#include <mongocxx/exception/exception.hpp>
#include <bsoncxx/builder/basic/document.hpp>
#include <bsoncxx/builder/basic/kvp.hpp>

using bsoncxx::builder::stream::close_document;
using bsoncxx::builder::stream::document;
using bsoncxx::builder::stream::finalize;
using bsoncxx::builder::stream::open_document;
using bsoncxx::builder::basic::kvp;
using bsoncxx::builder::basic::make_document;
Run Code Online (Sandbox Code Playgroud)

这是我尝试过的。

  void MyClass::on_querybtn_clicked()
   {

auto collection = conn["TestDB"]["fdevices"];
bsoncxx::types::b_regex::b_regex();//i am lost here dont know how to use it
auto cursor = collection.find({});

 for (auto doc : cursor) {

QString qstr = QString::fromStdString(bsoncxx::to_json(doc));
QJsonDocument docum = QJsonDocument::fromJson(qstr.toUtf8());
QJsonObject object = docum.object();
QString call = object["Data"].toString();
ui.mqttList->addItem(call);
}
}
Run Code Online (Sandbox Code Playgroud)

以前我使用 Java 构建了一个软件,现在我正在尝试使用 Qt c++ 构建相同的软件,我是 C++ 的新手。

这是我在 java 中用于查询的查询代码。

 DBObject query = new BasicDBObject();
 Pattern regex = Pattern.compile("^14-09-2017"); 
 query.put("Data", regex);
Run Code Online (Sandbox Code Playgroud)

这是我的数据的样子。 数据库图像

Nei*_*unn 4

使用一些构建器并创建文档,为模式提供字符串输入:

#include <bsoncxx/builder/basic/document.hpp>
#include <bsoncxx/builder/basic/kvp.hpp>

using bsoncxx::builder::basic::kvp;
using bsoncxx::builder::basic::make_document;

auto cursor = collection.find(make_document(
  kvp("Data", bsoncxx::types::b_regex{"^14-09-2017"})
));
Run Code Online (Sandbox Code Playgroud)

恕我直言,比流构建器更容易阅读,但基本前提是存在的。仍然是一个用于输入的字符串,很像Pattern.compile()