Specify a Query
On this page
Overview
In this guide, you can learn how to specify a query by using the C++ driver.
You can refine the set of documents that a query returns by creating a query filter. A query filter is an expression that specifies the search criteria that MongoDB uses to match documents in a read or write operation. In a query filter, you can prompt the driver to search for documents that have an exact match to your query, or you can compose query filters to express more complex matching criteria.
Sample Data
The examples in this guide run operations on a collection called
fruits
, which contains documents representing fruits. The following
code example shows how to create a database and collection, then
insert the sample documents into your collection:
mongocxx::uri uri("<connection string>"); mongocxx::client client(uri); auto db = client["db"]; auto collection = db["fruits"]; std::vector<bsoncxx::document::value> fruits; fruits.push_back(make_document(kvp("_id", 1), kvp("name", "apples"), kvp("qty", 5), kvp("rating", 3), kvp("color", "red"), kvp("type", make_array("fuji", "honeycrisp")))); fruits.push_back(make_document(kvp("_id", 2), kvp("name", "bananas"), kvp("qty", 7), kvp("rating", 4), kvp("color", "yellow"), kvp("type", make_array("cavendish")))); fruits.push_back(make_document(kvp("_id", 3), kvp("name", "oranges"), kvp("qty", 6), kvp("rating", 2), kvp("type", make_array("naval", "mandarin")))); fruits.push_back(make_document(kvp("_id", 4), kvp("name", "pineapples"), kvp("qty", 3), kvp("rating", 5), kvp("color", "yellow"))); auto result = collection.insert_many(fruits);
Note
mongocxx::instance
The code examples on this page assume that you've already created a mongocxx::instance
object elsewhere in your application.
To learn more about creating an instance, see Create a Driver Instance.
Exact Match
Literal value queries return documents that have an exact match to your query filter.
The following example specifies a query filter as a parameter to the find()
method. The code returns all documents in which the value of the color
field
is "yellow"
:
auto cursor = collection.find(make_document(kvp("color", "yellow"))); for (auto &&doc : cursor) { std::cout << bsoncxx::to_json(doc) << std::endl; }
{ "_id" : 2, "name" : "bananas", "qty" : 7, "rating" : 4, "color" : "yellow", "type" : [ "cavendish" ] } { "_id" : 4, "name" : "pineapple", "qty" : 3, "rating" : 5, "color" : "yellow" }
Tip
Find All Documents
To find all documents in a collection, call the find()
method and pass it an
empty query filter. The following example finds all documents in a
collection:
auto cursor = collection.find({});
Comparison Operators
Comparison operators evaluate a document field value against a specified value in your query filter. The following list defines common comparison operators:
$gt
: Greater than$lte
: Less than or Equal$ne
: Not equal
To view a full list of comparison operators, see the Comparison Query Operators guide in the MongoDB Server manual.
The following example specifies a comparison operator in a query filter as a
parameter to the find()
method. The code returns all documents that have a
rating
field value greater than 2
:
auto cursor = collection.find(make_document(kvp("rating", make_document(kvp("$gt", 2))))); for(auto&& doc : cursor) { std::cout << bsoncxx::to_json(doc) << std::endl; }
{ "_id" : 1, "name" : "apples", "qty" : 5, "rating" : 3, "color" : "red", "type" : [ "fuji", "honeycrisp" ] } { "_id" : 2, "name" : "bananas", "qty" : 7, "rating" : 4, "color" : "yellow", "type" : [ "cavendish" ] } { "_id" : 4, "name" : "pineapples", "qty" : 3, "rating" : 5, "color" : "yellow" }
Logical Operators
Logical operators match documents by using logic applied to the results of two or more sets of expressions. The following list describes each logical operator:
$and
: Returns all documents that match the conditions of all clauses$or
: Returns all documents that match the conditions of one clause$nor
: Returns all documents that do not match the conditions of any clause$not
: Returns all documents that do not match the expression
To learn more about logical operators, see the Logical Query Operators guide in the MongoDB Server manual.
The following example specifies a logical operator in a query filter as a
parameter to the find()
method. The code returns all documents in which the
qty
field value is greater than 5
or the color
field value is
"yellow"
:
auto cursor = collection.find( make_document(kvp("$or", make_array(make_document(kvp("qty", make_document(kvp("$gt", 5)))), make_document(kvp("color", "yellow")))))); for (auto&& doc : cursor) { std::cout << bsoncxx::to_json(doc) << std::endl; }
{ "_id" : 2, "name" : "bananas", "qty" : 7, "rating" : 4, "color" : "yellow", "type" : [ "cavendish" ] } { "_id" : 3, "name" : "oranges", "qty" : 6, "rating" : 2, "type" : [ "naval", "mandarin" ] } { "_id" : 4, "name" : "pineapples", "qty" : 3, "rating" : 5, "color" : "yellow" }
Array Operators
Array operators match documents based on the value or quantity of elements in an array field. The following list describes the available array operators:
$all
: Returns documents with arrays that contain all elements in the query$elemMatch
: Returns documents if an element in their array field matches all conditions in the query$size
: Returns all documents with arrays of a specified size
To learn more about array operators, see the Array Query Operators guide in the MongoDB Server manual.
The following example specifies an array operator in a query filter as a
parameter to the find()
method. The code returns all documents in which the
type
array field contains 2
elements:
auto cursor = collection.find(make_document(kvp("type", make_document(kvp("$size", 2))))); for(auto&& doc : cursor) { std::cout << bsoncxx::to_json(doc) << std::endl; }
{ "_id" : 1, "name" : "apples", "qty" : 5, "rating" : 3, "color" : "red", "type" : [ "fuji", "honeycrisp" ] } { "_id" : 3, "name" : "oranges", "qty" : 6, "rating" : 2, "type" : [ "naval", "mandarin" ] }
Element Operators
Element operators query data based on the presence or type of a field.
To learn more about element operators, see the Element Query Operators guide in the MongoDB Server manual.
The following example specifies an element operator in a query filter as a
parameter to the find()
method. The code returns all documents that have a
color
field:
auto cursor = collection.find(make_document(kvp("color", make_document(kvp("$exists", true))))); for(auto&& doc : cursor) { std::cout << bsoncxx::to_json(doc) << std::endl; }
{ "_id" : 1, "name" : "apples", "qty" : 5, "rating" : 3, "color" : "red", "type" : [ "fuji", "honeycrisp" ] } { "_id" : 2, "name" : "bananas", "qty" : 7, "rating" : 4, "color" : "yellow", "type" : [ "cavendish" ] } { "_id" : 4, "name" : "pineapples", "qty" : 3, "rating" : 5, "color" : "yellow" }
Evaluation Operators
Evaluation operators return data based on evaluations of either individual fields or the entire collection's documents.
The following list describes common evaluation operators:
$text
: Performs a text search on the documents$regex
: Returns documents that match a specified regular expression$mod
: Performs a modulo operation on the value of a field and returns documents where the remainder is a specified value
To view a full list of evaluation operators, see the Evaluation Query Operators guide in the MongoDB Server manual.
The following example specifies an evaluation operator in a query filter as a
parameter to the find()
method. The code uses a regular expression to return
all documents in which the name
field value has at least two consecutive
"p"
characters:
auto cursor = collection.find(make_document(kvp("name", make_document(kvp("$regex", "p{2,}"))))); for(auto&& doc : cursor) { std::cout << bsoncxx::to_json(doc) << std::endl; }
{ "_id" : 1, "name" : "apples", "qty" : 5, "rating" : 3, "color" : "red", "type" : [ "fuji", "honeycrisp" ] } { "_id" : 4, "name" : "pineapples", "qty" : 3, "rating" : 5, "color" : "yellow" }
Additional Information
To learn more about querying documents, see the Query Documents guide in the MongoDB Server manual.
To learn more about retrieving documents with the C++ driver, see the Retrieve Data guide.
API Documentation
To learn more about any of the methods or types discussed in this guide, see the following API documentation: