...
The find-query is based on the syntax of the MongoDB JavaScript driver (http://mongodb.github.io/node-mongodb-native/api-generated/collection.html#find). At least one query object is required. Optionally you can also specify an options object.
...
The find-query is based on the syntax of the MongoDB JavaScript driver (http://mongodb.github.io/node-mongodb-native/api-generated/collection.html#find). At least one query object is required. Optionally you can also specify an options object.
Codeblock |
---|
|
{query}[, {options}] |
A special feature of Mongo Management Studio is to enter the queries as quickly and easily as possible. The following example queries refer to the zipcode collection zipcode from the Getting started tutorial.
Codeblock |
---|
language | js |
---|
theme | Emacs |
---|
language | js |
---|
|
// Search all cities in the U.S. State of California. (CA)
// all the following queries return the same result
// standard MongoDB query
{state: "CA"}
// If there is no options object, the curly braces can be omitted
state: "CA"
// You can also write everything in quotation marks
"state": "CA"
// Or use single quotes
state: 'CA' |
Hinweis |
---|
MongoDB queries always distinguishes between upper and lowercase. To work around this, you can use Regular Expressions. |
Of course it is possible to make more complex queries in this manner.
Codeblock |
---|
language | js |
---|
theme | Emacs | language | js |
---|
|
//Find all cities in the U.S. State of Alabama(AL) with more than 40,000 inhabitants
// standard MongoDB query
{
state: "AL",
pop: {
$gt: 40000
}
}
// shortened query
state: "AL", pop: {$gt: 40000}
// shortened query without whitespaces
state:"AL",pop:{$gt:40000} |
Using options
Codeblock |
---|
language | js |
---|
theme | Emacs | language | js |
---|
|
// Find all cities in the U.S. State of Alabama(AL) with more than 40,000 inhabitants and return only the names of the cities as result
{state: "AL", pop: {$gt: 40000}}, {fields: {city: 1}}
// Array Syntax
{state: "AL", pop: {$gt: 40000}}, {fields: ['city']}
// also id field leave out
{state: "AL", pop: {$gt: 40000}}, {fields: {city: 1, _id: 0}}
// Find all cities in the U.S. State of Alabama(AL) with a population of more than 10,000 and sort in ascending order by city name
{state: "AL", pop: {$gt: 10000}}, {sort: {city: 1}}
// Find all cities in the U.S. State of Alabama(AL) with a population of more than 10,000 and return 10 documents, whereby the first 20 documents are skipped
{state: "AL", pop: {$gt: 10000}}, {limit: 10, skip: 20} |
Hinweis |
---|
When using an options object curly brackets must be used in the query object. |
Tipp |
---|
In der Tabellenansicht kann auch Sortieren, ohne dass man eine sort option verwenden muss. Dazu genügt es, auf den Namen einer Spalte zu klicken, um nach dieser zu sortieren. Beim nochmaligen Klick auf den Spaltennamen wird die Sortierung umgekehrt. |
Spezielle Datentypen
ObjectId
Folgende Abfragen liefern jeweils das gleiche Ergebnis, wenn man nach einer bestimmten ObjectId sucht
Codeblock |
---|
|
theme | Emacs |
---|
language | js documents are skipped
{state: "AL", pop: {$gt: 10000}}, {limit: 10, skip: 20} |
Hinweis |
---|
When using an options object curly brackets must be used in the query object. |
Hinweis |
---|
If you want to use "$elemMatch" with ObjectIds or ISODates in the options (see example), you must use the console mode. Codeblock |
---|
| {}, {fields: {match: {$elemMatch: {a: ObjectId("53ea1f663b6f20cc19001002")}}}} |
|
Tipp |
---|
In the table view you can also sort without having to use a sort option. For this purpose, it is enough to click on the name of a column to sort by that. The sorting is reversed by repeated clicking on the column name. |
Special data types
ObjectId
The following queries will each return the same result if you are looking for a specific ObjectId
Codeblock |
---|
|
{_id: ObjectId('52bd27ebf370afe931001364')}
{_id: ObjectId("52bd27ebf370afe931001364")}
_id: ObjectId('52bd27ebf370afe931001364')
_id: ObjectId("52bd27ebf370afe931001364")
_id: "52bd27ebf370afe931001364"
_id: '52bd27ebf370afe931001364' |
Hinweis |
---|
Mongo Management Studio erkennt Strings, die das Format einer MongoDB ObjectId haben und konvertiert dieses dann. Sollte es passieren, dass in einer Collection _ids als Strings gespeichert und diese MongoDB ObjectId konform sind, muss man für die Suchabfrage über den Konsolenmodus verwenden. Ansonsten werden keine Daten gefunden. coderecognizes strings,having the format of a MongoDB ObjectId and converts them. Should it happen that _ids stored as strings in a collection and they are compliant to MongoDB ObjectId, you have to use the Console mode for the query. Otherwise, no data is found. Codeblock |
---|
language | js |
---|
theme | Emacs |
---|
language | js |
---|
| // findefind Personpersons mitwith id '52bd27ebf370afe931001364'
db.persons.find{_id: '52bd27ebf370afe931001364'}
|
|
Datumswerte
Datumswerte müssen in einer bestimmten Syntax eingegeben werden, da diese nicht automatisch konvertiert werden können.
...
...
Date values
Date values must be entered in a specific syntax because they can´t be converted automatically.
Codeblock |
---|
|
// as ISO-String
start: ISODate('2013-08-02T08:20:38.993Z')
// alsas Datumsdate-String
start: {$gte: {ISODate('dec 11, 1989')}}
start: {$gte: {ISODate("1989, 12, 11")}}
// alsas timestamp in ms
start: ISODate(629379488000) |
Hinweis |
---|
Codeblock |
---|
|
theme | Emacs |
---|
language | jsJavaScript arbeitet in Millisekunden, UNIX timestamps müssen mit 1000 multipliziert werden, um korrekt umgerechnet oder angezeigt werden zu können. |
Regex als Wertzuweisung eines Datenfeldes
Besitzt ein Feld eines Datensatzes als Wert einen Regex-Ausdruck kann man diesen wie folgt abfragen.
works in milliseconds, UNIX timestamps must be multiplied by 1000 to be correctly converted or displayed. |
Regex as a data field value assignment
If a field of a document has a value with a regex expression, you can this query as follows.
Hinweis |
---|
Es muss der komplette Regex-Ausdruck angegeben werden, um den entsprechenen Datensatz mit dem richtigen Wert zu erhaltenYou must specify the complete regex expression to obtain the corresponding document with the correct value. |
Regex
...
Man kann Regex auch verwenden, um Felder abzufragen.
...
queries for fields with strings
You can also use regex to query fields.
Codeblock |
---|
language | js |
---|
theme | Emacs | language | js |
---|
|
// Suchefind alleall Städtecities, diebeginning mitwith 'FLO'
beginnen
city: {$regex: '^FLO'}
// ignoriereignore Groß-upper undand Kleinschreibunglowercase
city: {$regex: '^flo', $options: 'i'}
// kurzeshortened Schreibweisequery
city: /^flo/i |
Hinweis |
---|
In der derzeitigen Version werden lediglich die Parameter the current version only the parameter "i" sowie and "m" als Optionen bei der Kurzschreibweise unterstützt. Die Parameter are supported as options in the short notation. The parameter "s" und and "x" folgen in einer späteren Versionfollow in a later version. |
Tipp |
---|
Genauere Informationen zum Gebrauch von Regex-Ausdrücken in Abfragen kann man hier nachlesen Here you can read more detailed information about the use of regex expressions in queries: http://docs.mongodb.org/manual/reference/operator/query/regex/ |