Zum Ende der Metadaten springen
Zum Anfang der Metadaten

Sie zeigen eine alte Version dieser Seite an. Zeigen Sie die aktuelle Version an.

Unterschiede anzeigen Seitenhistorie anzeigen

« Vorherige Version anzeigen Version 10 Aktuelle »

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.

{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 from the Getting started tutorial.

// 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'

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.

//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

// 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}

When using an options object curly brackets must be used in the query object.

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

{}, {fields: {match: {$elemMatch: {a: ObjectId("53ea1f663b6f20cc19001002")}}}}

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

{_id: ObjectId('52bd27ebf370afe931001364')}
{_id: ObjectId("52bd27ebf370afe931001364")}
_id: ObjectId('52bd27ebf370afe931001364')
_id: ObjectId("52bd27ebf370afe931001364")
_id: "52bd27ebf370afe931001364"
_id: '52bd27ebf370afe931001364'

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.

// finde Person mit id '52bd27ebf370afe931001364'
db.persons.find{_id: '52bd27ebf370afe931001364'}

Date values

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

// 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)

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.

pattern: /abc/

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

Regex queries for fields with strings

You can also use regex to query fields.

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

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.

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

  • Keine Stichwörter