Rechercher dans le manuel MySQL
20.3.3.3 Find Documents
You can use the find()
method to query for
and return documents from a collection in a schema.
MySQL Shell provides additional methods to use with the
find()
method to filter and sort the returned
documents.
MySQL provides the following operators to specify search
conditions: OR
(||
),
AND
(&&
),
XOR
, IS
,
NOT
, BETWEEN
,
IN
, LIKE
,
!=
, <>
,
>
, >=
,
<
, <=
,
&
, |
,
<<
, >>
,
+
, -
,
*
, /
,
~
, and %
.
Find All Documents in a Collection
To return all documents in a collection, use the
find()
method without specifying search
conditions. For example, the following operation returns all
documents in the countryinfo
collection.
mysql-js> db.countryinfo.find()
[
{
"GNP": 828,
"IndepYear": null,
"Name": "Aruba",
"_id": "ABW",
"demographics": {
"LifeExpectancy": 78.4000015258789,
"Population": 103000
},
"geography": {
"Continent": "North America",
"Region": "Caribbean",
"SurfaceArea": 193
},
"government": {
"GovernmentForm": "Nonmetropolitan Territory of The Netherlands",
"HeadOfState": "Beatrix"
}
...
}
]
240 documents in set (0.00 sec)
The method produces results that contain operational information in addition to all documents in the collection.
An empty set (no matching documents) returns the following information:
Empty set (0.00 sec)
You can include search conditions with the
find()
method. The syntax for expressions
that form a search condition is the same as that of
traditional MySQL Chapter 12, Functions and Operators. You must
enclose all expressions in quotes. For the sake of brevity,
some of the examples do not display output.
A simple search condition could consist of the
Name
field and a value we know is in a
document. The following example returns a single document:
mysql-js> db.countryinfo.find("Name = 'Australia'")
[
{
"GNP": 351182,
"IndepYear": 1901,
"Name": "Australia",
"_id": "AUS",
"demographics": {
"LifeExpectancy": 79.80000305175781,
"Population": 18886000
},
"geography": {
"Continent": "Oceania",
"Region": "Australia and New Zealand",
"SurfaceArea": 7741220
},
"government": {
"GovernmentForm": "Constitutional Monarchy, Federation",
"HeadOfState": "Elisabeth II"
}
}
]
The following example searches for all countries that have a
GNP higher than $500 billion. The
countryinfo
collection measures GNP in
units of million.
mysql-js> db.countryinfo.find("GNP > 500000")
...[output removed]
10 documents in set (0.00 sec)
The Population field in the following query is embedded within the demographics object. To access the embedded field, use a period between demographics and Population to identify the relationship. Document and field names are case-sensitive.
mysql-js> db.countryinfo.find("GNP > 500000 and demographics.Population < 100000000")
...[output removed]
6 documents in set (0.00 sec)
Arithmetic operators in the following expression are used to query for countries with a GNP per capita higher than $30000. Search conditions can include arithmetic operators and most MySQL functions.
Seven documents in the countryinfo
collection have a population value of zero. Therefore
warning messages appear at the end of the output.
mysql-js> db.countryinfo.find("GNP*1000000/demographics.Population > 30000")
...[output removed]
9 documents in set, 7 warnings (0.00 sec)
Warning (Code 1365): Division by 0
Warning (Code 1365): Division by 0
Warning (Code 1365): Division by 0
Warning (Code 1365): Division by 0
Warning (Code 1365): Division by 0
Warning (Code 1365): Division by 0
Warning (Code 1365): Division by 0
You can separate a value from the search condition by using
the bind()
method. For example, instead of
specifying a hard-coded country name as the condition,
substitute a named placeholder consisting of a colon followed
by a name that begins with a letter, such as
country. Then use the
bind(
method as follows:
placeholder
,
value
)
mysql-js> db.countryinfo.find("Name = :country").bind("country", "Italy")
[
{
"GNP": 1161755,
"IndepYear": 1861,
"Name": "Italy",
"_id": "ITA",
"demographics": {
"LifeExpectancy": 79,
"Population": 57680000
},
"geography": {
"Continent": "Europe",
"Region": "Southern Europe",
"SurfaceArea": 301316
},
"government": {
"GovernmentForm": "Republic",
"HeadOfState": "Carlo Azeglio Ciampi"
}
}
]
1 document in set (0.01 sec)
Within a program, binding enables you to specify placeholders in your expressions, which are filled in with values before execution and can benefit from automatic escaping, as appropriate.
Always use binding to sanitize input. Avoid introducing values in queries using string concatenation, which can produce invalid input and, in some cases, can cause security issues.
You can use placeholders and the bind()
method to create saved searches which you can then call with
different values. For example to create a saved search for a
country:
mysql-js> var myFind = db.countryinfo.find("Name = :country")
mysql-js> myFind.bind('country', 'France')
[
{
"GNP": 1424285,
"IndepYear": 843,
"Name": "France",
"_id": "FRA",
"demographics": {
"LifeExpectancy": 78.80000305175781,
"Population": 59225700
},
"geography": {
"Continent": "Europe",
"Region": "Western Europe",
"SurfaceArea": 551500
},
"government": {
"GovernmentForm": "Republic",
"HeadOfState": "Jacques Chirac"
}
}
]
1 document in set (0.0028 sec)
mysql-js> myFind.bind('country', 'Germany')
[
{
"GNP": 2133367,
"IndepYear": 1955,
"Name": "Germany",
"_id": "DEU",
"demographics": {
"LifeExpectancy": 77.4000015258789,
"Population": 82164700
},
"geography": {
"Continent": "Europe",
"Region": "Western Europe",
"SurfaceArea": 357022
},
"government": {
"GovernmentForm": "Federal Republic",
"HeadOfState": "Johannes Rau"
}
}
]
1 document in set (0.0026 sec)
You can return specific fields of a document, instead of
returning all the fields. The following example returns the
GNP and Name fields of all documents in the
countryinfo
collection matching the search
conditions.
Use the fields()
method to pass the list of
fields to return.
mysql-js> db.countryinfo.find("GNP > 5000000").fields(["GNP", "Name"])
[
{
"GNP": 8510700,
"Name": "United States"
}
]
1 document in set (0.00 sec)
In addition, you can alter the returned documents—adding, renaming, nesting and even computing new field values—with an expression that describes the document to return. For example, alter the names of the fields with the following expression to return only two documents.
mysql-js> db.countryinfo.find().
fields(mysqlx.expr('{"Name": upper(Name), "GNPPerCapita": GNP*1000000/demographics.Population}')).
limit(2)
[
{
"GNPPerCapita": 8038.834951456311,
"Name": "ARUBA"
},
{
"GNPPerCapita": 263.0281690140845,
"Name": "AFGHANISTAN"
}
]
2 documents in set (0.00 sec)
You can apply the limit()
,
sort()
, and skip()
methods to manage the number and order of documents returned
by the find()
method.
To specify the number of documents included in a result set,
append the limit()
method with a value to
the find()
method. The following query
returns the first five documents in the
countryinfo
collection.
mysql-js> db.countryinfo.find().limit(5)
... [output removed]
5 documents in set (0.00 sec)
To specify an order for the results, append the
sort()
method to the
find()
method. Pass to the
sort()
method a list of one or more fields
to sort by and, optionally, the descending
(desc
) or ascending
(asc
) attribute as appropriate. Ascending
order is the default order type.
For example, the following query sorts all documents by the IndepYear field and then returns the first eight documents in descending order.
mysql-js> db.countryinfo.find().sort(["IndepYear desc"]).limit(8)
... [output removed]
8 documents in set (0.00 sec)
By default, the limit()
method starts from
the first document in the collection. You can use the
skip()
method to change the starting
document. For example, to ignore the first document and return
the next eight documents matching the condition, pass to the
skip()
method a value of 1.
mysql-js> db.countryinfo.find().sort(["IndepYear desc"]).limit(8).skip(1)
... [output removed]
8 documents in set (0.00 sec)
The MySQL Reference Manual provides detailed documentation on functions and operators.
See CollectionFindFunction for the full syntax definition.
Traduction non disponible
Le manuel MySQL n'est pas encore traduit en français sur l'infobrol. Seule la version anglaise est disponible pour l'instant.
Document créé le 26/06/2006, dernière modification le 26/10/2018
Source du document imprimé : https://www.gaudry.be/mysql-rf-mysql-shell-tutorial-javascript-documents-find.html
L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.
Références
Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.