Versionen im Vergleich

Schlüssel

  • Diese Zeile wurde hinzugefügt.
  • Diese Zeile wurde entfernt.
  • Formatierung wurde geändert.

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.

code
Codeblock
language
js
themeEmacslanguagejs
{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
languagejs
themeEmacslanguagejs
// 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
languagejs
themeEmacs
languagejs
//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
languagejs
themeEmacslanguagejs
// 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

If you want to use "$elemMatch" with ObjectIds or ISODates in the options (see example), you must use the console mode.

Codeblock
language
languagejs
themeEmacsjs
{}, {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
languagejs
themeEmacs
languagejs
{_id: ObjectId('52bd27ebf370afe931001364')}
{_id: ObjectId("52bd27ebf370afe931001364")}
_id: ObjectId('52bd27ebf370afe931001364')
_id: ObjectId("52bd27ebf370afe931001364")
_id: "52bd27ebf370afe931001364"
_id: '52bd27ebf370afe931001364'
Hinweis

Mongo Management Studio recognizes 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
languagejs
themeEmacsjs
// findefind Personpersons mitwith id '52bd27ebf370afe931001364'
db.persons.find{_id: '52bd27ebf370afe931001364'}

...

Date values must be entered in a specific syntax because they can´t be converted automatically.

Codeblock
languagejs
themeEmacslanguagejs
// as ISO-String
start: ISODate('2013-08-02T08:20:38.993Z')
 
// as date-String
start: {$gte: {ISODate('dec 11, 1989')}}
start: {$gte: {ISODate("1989, 12, 11")}}
 
// as timestamp in ms
start: ISODate(629379488000)
Hinweis

JavaScript 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.

Codeblock
languagejs
themeEmacs
languagejs
pattern: /abc/
Hinweis

You must specify the complete regex expression to obtain the corresponding document with the correct value.

Anker
regex
regex

Regex queries for fields with strings

You can also use regex to query fields.

Codeblock
languagejs
themeEmacslanguagejs
// find all cities, beginning with 'FLO'
city: {$regex: '^FLO'}
 
// ignore upper and lowercase
city: {$regex: '^flo', $options: 'i'}
 
// shortened query
city: /^flo/i
Hinweis

In the current version only the parameter "i" and "m" are supported as options in the short notation. The parameter "s" and "x" follow in a later version.

Tipp

Here you can read more detailed information about the use of regex expressions in queries: http://docs.mongodb.org/manual/reference/operator/query/regex/