mongoDB Query文档

Querying

One of MongoDB's best capabilities is its support for dynamic (ad hoc) queries. Systems that support dynamic queries don't require any special indexing to find data; users can find data using any criteria. For relational databases, dynamic queries are the norm. If you're moving to MongoDB from a relational databases, you'll find that many SQL queries translate easily to MongoDB's document-based query language.

Query Expression Objects

MongoDB supports a number of query objects for fetching data. Queries are expressed as BSON documents which indicate a query pattern. For example, suppose we're using the MongoDB shell and want to return every document in the users collection. Our query would look like this:

 db.users.find({})

 In this case, our selector is an empty document, which matches every document in the collection. Here's a more selective example:

 db.users.find({'last_name': 'Smith'})
 

Here our selector will match every document where the last_name attribute is 'Smith.'MongoDB support a wide array of possible document selectors. For more examples, see the MongoDB Tutorial or the section on Advanced Queries . If you're working with MongoDB from a language driver, see the driver docs:

Related driver docs: Python , Java , Ruby , PHP , Perl

Query Options

Field Selection

In addition to the query expression, MongoDB queries can take some additional arguments. For example, it's possible to request only certain fields be returned. If we just wanted the social security numbers of users with the last name of 'Smith,' then from the shell we could issue this query:

// retrieve ssn field for documents where last_name == 'Smith':
  db.users.find({last_name: 'Smith'}, {'ssn': 1});

  // retrieve all fields *except* the thumbnail field, for all documents:
  db.users.find({}, {thumbnail:0});

Sorting

MongoDB queries can return sorted results. To return all documents and sort by last name in ascending order, we'd query like so:

 db.users.find({}).sort({last_name: 1});

Skip and Limit

MongoDB also supports skip and limit for easy paging. Here we skip the first 20 last names, and limit our result set to 10:

db.users.find().skip(20).limit(10);
db.users.find({}, {}, 10, 20); // same as above, but less clear

slaveOk (Querying Secondaries)

When querying a replica set, drivers route their requests to the primary mongod by default; to perform a query against an (arbitrarily-selected) secondary, the query can be run with the slaveOk option. See the slaveOk page for more information.

Cursors

Database queries, performed with the find() method, technically work by returning a cursor . Cursors are then used to iteratively retrieve all the documents returned by the query. For example, we can iterate over a cursor in the mongo shell like this:

> var cur = db.example.find();
> cur.forEach( function(x) { print(tojson(x))});
{"n" : 1 , "_id" : "497ce96f395f2f052a494fd4"}
{"n" : 2 , "_id" : "497ce971395f2f052a494fd5"}
{"n" : 3 , "_id" : "497ce973395f2f052a494fd6"}
>

Mongo Query Language

Queries in MongoDB are expressed as JSON (BSON). Usually we think of query object as the equivalent of a SQL "WHERE" clause:

> db.users.find( { x : 3, y : "abc" } ).sort({x:1}); // select * from users where x=3 and y='abc' order by x asc;

However, the MongoDB server actually looks at all the query parameters (ordering, limit, etc.) as a single object. In the above example from the mongo shell, the shell is adding some syntactic sugar for us. Many of the drivers do this too. For example the above query could also be written:

> db.users.find( { $query : { x : 3, y : "abc" }, $orderby : { x : 1 } } );
 

The possible specifies in the query object are:

  • $query - the evaluation or "where" expression
  • $orderby - sort order desired
  • $hint - hint to query optimizer
  • $explain - if true, return explain plan results instead of query results
  • $snapshot - if true, "snapshot mode"

参考:http://www.mongodb.org/display/DOCS/Querying#Querying-QueryExpressionObjects

猜你喜欢

转载自san-yun.iteye.com/blog/1720332