Mongocxx:如何反向显示文档
Mongocxx: How to display the documents inversely
我试图反向显示集合的文档。在shell中,这可以通过使用以下命令来完成:
db.testcollection.find().sort({$natural:-1})
在文档中我发现了这个函数:
void sort(bsoncxx::document::view_or_value ordering);
/// The order in which to return matching documents. If $orderby also exists in the modifiers
/// document, the sort field takes precedence over $orderby.
///
/// @param ordering
/// Document describing the order of the documents to be returned.
///
/// @see http://docs.mongodb.org/manual/reference/method/cursor.sort/
如何像shell示例一样将自然值设置为-1 ?谢谢!
您需要使用bsoncxx
构建器创建排序顺序文档。下面是插入10个文档并按倒序转储它们的示例:
#include <iostream>
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/document/value.hpp>
#include <bsoncxx/document/view.hpp>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/collection.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/options/find.hpp>
#include <mongocxx/uri.hpp>
using namespace bsoncxx;
int main() {
auto inst = mongocxx::instance{};
auto client = mongocxx::client{mongocxx::uri{}};
auto coll = client["test"]["sorttest"];
coll.drop();
for (auto i = 0; i < 10; i++) {
coll.insert_one(builder::stream::document{} << "seq" << i << builder::stream::finalize);
}
auto order = builder::stream::document{} << "$natural" << -1 << builder::stream::finalize;
auto opts = mongocxx::options::find{};
opts.sort(order.view());
auto cursor = coll.find({}, opts);
for (auto&& doc : cursor) {
std::cout << to_json(doc) << std::endl;
}
}
相关文章:
- 正在查找文档以获得PS4平台的C++中的设备信息
- 如何在文档文件夹中创建目录
- 当Microsoft文档仅包含 C# 示例时,如何查找 C++ 包含文件名
- 通过构造函数创建一些值并尝试添加到文档中使用 rapidjson 不起作用
- 关于类的 Python 文档 - 对C++的引用不正确
- C++/autoconf 等效于文档库要求.txt的要求是什么?
- mbed:使用 USB 文档库编译会导致错误
- C++ VISUAL STUDIO:GLFWwindow没有初始化,即使它是在文档中编写的并且以前工作过
- 使用 yaml-cpp 更新 YAML 文档的节点和值
- 在加载 MSHTML 文档之前从 MSHTML 文档中删除无效的 URL
- 如何在C++中表示JSON文档的递归对象结构?
- 如何查找 Redshift Maya API 文档?
- 如何使用值模板编写文档测试用例?
- Mongocxx:如何反向显示文档
- c++ MFC MDI如何根据活动文档更改类视图显示的内容
- VS2012如何通过智能感知显示标准c++库的文档
- 为什么从文档 OpenCV 剂量中找到的源代码人脸检测没有显示任何东西
- 如果同时运行两次wkhtmltopdf库,为什么它只会发生数据文档,而没有任何表或图像显示在html文件中
- MFC如何通知文档被更改(并显示*)
- 显示同一文档的多个同时视图