指定返回的文档
概述
在本指南中,您可以通过以下方法了解如何指定从读取操作返回哪些文档:
mongocxx::options::find::limit():指定从查询返回的文档的最大数量。
mongocxx::options::find::sort():指定返回文档的排序顺序。
mongocxx::options::find::skip():指定在返回查询结果之前要跳过的文档数量。
示例数据
本指南中的示例使用了restaurants
集合,该集合位于 sample_restaurants
数据库中,数据来源于 Atlas 示例数据集。要从您的 C++ 应用程序访问此集合,请创建一个连接到 Atlas 集群的 mongocxx::client
实例,并将以下值分配给您的 db
和 collection
变量
auto db = client["sample_restaurants"]; auto collection = db["restaurants"];
有关如何创建免费的 MongoDB Atlas 集群并加载示例数据集的说明,请参阅 Atlas 入门指南。
限制
要指定从读取操作返回的最大文档数,创建一个 mongocxx::options::find
类的实例,并设置其 limit
字段。然后,将您的 mongocxx::options::find
实例作为参数传递给 find()
方法。
以下示例查找所有具有 cuisine
字段值为 "Italian"
的餐馆,并将结果限制为 5
个文档。
mongocxx::options::find opts{}; opts.limit(5); auto cursor = collection.find(make_document(kvp("cuisine", "Italian")), opts); for(auto&& doc : cursor) { std::cout << bsoncxx::to_json(doc) << std::endl; }
{ "_id" : { "$oid" : "..." }, ..., "name" : "Philadelphia Grille Express", "restaurant_id" : "40364305" } { "_id" : { "$oid" : "..." }, ..., "name" : "Isle Of Capri Restaurant", "restaurant_id" : "40364373" } { "_id" : { "$oid" : "..." }, ..., "name" : "Marchis Restaurant", "restaurant_id" : "40364668" } { "_id" : { "$oid" : "..." }, ..., "name" : "Crystal Room", "restaurant_id" : "40365013" } { "_id" : { "$oid" : "..." }, ..., "name" : "Forlinis Restaurant", "restaurant_id" : "40365098" }
提示
前面的示例根据数据库中的自然顺序返回前五个与查询匹配的文档。下一节将描述如何以指定顺序返回文档。
排序
要以指定顺序返回文档,创建一个包含用于排序结果的字段和排序方向的文档。值为 1
从最低到最高排序值,值为 -1
从最高到最低排序。然后,在 mongocxx::options::find
实例上调用 mongocxx::options::find::sort()
方法,并将此文档作为参数传递。
以下示例返回所有具有 cuisine
值为 "Italian"
的文档,并按 name
字段值的升序排序。
mongocxx::options::find opts{}; opts.sort(make_document(kvp("name", 1))); auto cursor = collection.find(make_document(kvp("cuisine", "Italian")), opts); for(auto&& doc : cursor) { std::cout << bsoncxx::to_json(doc) << std::endl; }
{ "_id" : { "$oid" : "..." }, ..., "name" : "(Lewis Drug Store) Locanda Vini E Olii", "restaurant_id" : "40804423" } { "_id" : { "$oid" : "..." }, ..., "name" : "101 Restaurant And Bar", "restaurant_id" : "40560108" } { "_id" : { "$oid" : "..." }, ..., "name" : "44 Sw Ristorante & Bar", "restaurant_id" : "40698807" } ... { "_id" : { "$oid" : "..." }, ..., "name" : "Zucchero E Pomodori", "restaurant_id" : "41189590" }
跳过
要跳过指定数量的文档以返回查询结果,创建一个 mongocxx::options::find
类的实例,并设置其 skip
字段。然后,将您的 mongocxx::options::find
实例作为参数传递给 find()
方法。
以下示例返回所有具有 borough
字段值为 "Manhattan"
的文档,并跳过前 10
个文档。
mongocxx::options::find opts{}; opts.skip(10); auto cursor = collection.find(make_document(kvp("borough", "Manhattan")), opts); for(auto&& doc : cursor) { std::cout << bsoncxx::to_json(doc) << std::endl; }
{ "_id" : { "$oid" : "..." }, ..., "name" : "Cafe Metro", "restaurant_id" : "40363298" } { "_id" : { "$oid" : "..." }, ..., "name" : "Lexler Deli", "restaurant_id" : "40363426" } { "_id" : { "$oid" : "..." }, ..., "name" : "Domino'S Pizza", "restaurant_id" : "40363644" } ...
合并限制、排序和跳过
您可以为单个 mongocxx::options::find
实例设置 limit
、sort
和 skip
字段。这允许您设置返回的排序文档的最大数量,并在返回之前跳过指定数量的文档。
以下示例返回具有 cuisine
值为 "Italian"
的 5
个文档。结果按 name
字段值升序排序,跳过前 10
个文档。
mongocxx::options::find opts{}; opts.sort(make_document(kvp("name", 1))).limit(5).skip(10); auto cursor = collection.find(make_document(kvp("cuisine", "Italian")), opts); for(auto&& doc : cursor) { std::cout << bsoncxx::to_json(doc) << std::endl; }
{ "_id" : { "$oid" : "..." }, ..., "name" : "Acqua", "restaurant_id" : "40871070" } { "_id" : { "$oid" : "..." }, ..., "name" : "Acqua Restaurant", "restaurant_id" : "41591488" } { "_id" : { "$oid" : "..." }, ..., "name" : "Acqua Santa", "restaurant_id" : "40735858" } { "_id" : { "$oid" : "..." }, ..., "name" : "Acquista Trattoria", "restaurant_id" : "40813992" } { "_id" : { "$oid" : "..." }, ..., "name" : "Acquolina Catering", "restaurant_id" : "41381423" }
注意
调用这些方法的顺序不会改变返回的文档。驱动程序会自动重新排序调用,首先执行排序操作,然后是跳过操作,最后是限制操作。
更多信息
有关检索文档的更多信息,请参阅检索数据指南。
有关指定查询的更多信息,请参阅指定查询指南。
API 文档
要了解更多关于本指南中讨论的任何方法或类型,请参阅以下API文档