Mongodb c++ 正则表达式查询

Mongodb c++ regex query

如何在 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;

这是我试过的。

  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);
}
}

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

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

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

这是我的数据的样子。 Database image

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

#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"})
));

恕我直言,比流生成器更容易阅读,但基本前提就在那里。仍然是输入的字符串,很像 Pattern.compile()

我尝试了几种方法,但最后我想出了最直接的一种。我将查询构建为 json,然后使用 bsoncxx::from_json 函数将其转换为 bson。 一段有效的代码如下所示:

 mongocxx::instance instance{};
 mongocxx::uri uri("mongodb://localhost:27017");
 mongocxx::client *m_c = new mongocxx::client(uri);    
 std::string query = "{\"name.firstName\": {\"$options\": \"i\",\"$regex\": \"^pattern\"}}";
 bsoncxx::builder::stream::document doc;
 //this one to define q not inside try catch block.
 bsoncxx::document::value q(doc.view());
 try{
   q = bsoncxx::from_json(query);

 }catch (bsoncxx::exception &e){
    std::cerr<<"error: "<<e.code()<<" "<<e.what();
    return;
 }
 mongocxx::options::find opt;
 mongocxx::cursor cursor = m_c->database("db_name").collection("collection_name").find( std::move(q), opt);

这里重要的是我不能将 q 用作 bsoncxx::document::view。 因为这个视图的值是在 try - catch 块中创建的,直到 当控制流到达查找函数时 bsoncxx::document::view q 总是空的。所以我不得不在查找函数中使用 bsoncxx::document::value q 和 move 语义。

#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("field",make_document(kvp("$regex":"pattern"),kvp("$options","i"))));

Refer To Official