Rechercher dans le manuel MySQL
20.4.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-py> 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-py> 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-py> 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-py> 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-py> 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-py> 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-py> myFind = db.countryinfo.find("Name = :country")
mysql-py> 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-py> 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-py> 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-py> 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-py> 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-py> 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-py> 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.
Deutsche Übersetzung
Sie haben gebeten, diese Seite auf Deutsch zu besuchen. Momentan ist nur die Oberfläche übersetzt, aber noch nicht der gesamte Inhalt.Wenn Sie mir bei Übersetzungen helfen wollen, ist Ihr Beitrag willkommen. Alles, was Sie tun müssen, ist, sich auf der Website zu registrieren und mir eine Nachricht zu schicken, in der Sie gebeten werden, Sie der Gruppe der Übersetzer hinzuzufügen, die Ihnen die Möglichkeit gibt, die gewünschten Seiten zu übersetzen. Ein Link am Ende jeder übersetzten Seite zeigt an, dass Sie der Übersetzer sind und einen Link zu Ihrem Profil haben.
Vielen Dank im Voraus.
Dokument erstellt 26/06/2006, zuletzt geändert 26/10/2018
Quelle des gedruckten Dokuments:https://www.gaudry.be/de/mysql-rf-mysql-shell-tutorial-python-documents-find.html
Die Infobro ist eine persönliche Seite, deren Inhalt in meiner alleinigen Verantwortung liegt. Der Text ist unter der CreativeCommons-Lizenz (BY-NC-SA) verfügbar. Weitere Informationen auf die Nutzungsbedingungen und dem Autor.
Referenzen
Diese Verweise und Links verweisen auf Dokumente, die während des Schreibens dieser Seite konsultiert wurden, oder die zusätzliche Informationen liefern können, aber die Autoren dieser Quellen können nicht für den Inhalt dieser Seite verantwortlich gemacht werden.
Der Autor Diese Website ist allein dafür verantwortlich, wie die verschiedenen Konzepte und Freiheiten, die mit den Nachschlagewerken gemacht werden, hier dargestellt werden. Denken Sie daran, dass Sie mehrere Quellinformationen austauschen müssen, um das Risiko von Fehlern zu reduzieren.