The find-query is based on 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.
...
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 collection zipcode from the Getting started tutorial.
Codeblock |
---|
|
// 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 |
---|
|
//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 |
---|
|
// 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
...
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 gefundenrecognizes 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 |
---|
| // finde Person mit id '52bd27ebf370afe931001364'
db.persons.find{_id: '52bd27ebf370afe931001364'}
|
|
Datumswerte
...
Date values
Date values must be entered in a specific syntax because they can´t be converted automatically.
Codeblock |
---|
|
// alsas 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 |
---|
JavaScript 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
...
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-Abfragen für Felder mit Strings
...