`
san_yun
  • 浏览: 2639721 次
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

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

 

分享到:
评论

相关推荐

    Spring Data MongoDB中文文档

    ### Spring Data MongoDB中文文档知识点概览 #### 一、Spring Data MongoDB概述 - **Spring Data MongoDB** 是 **Spring Data** 家族的一员,它提供了一种简单的方式来与 MongoDB 数据库进行交互。通过 Spring Data...

    springdata mongodb api文档

    当需要扩展SpringData的功能时,SpringData MongoDB也提供了一定的扩展机制,例如使用Query DSL扩展进行复杂查询,或者使用Web支持来构建基于SpringData MongoDB的Web应用。 对于那些希望了解如何使用SpringData ...

    MongoDB删除文档.pdf

    MongoDB 删除文档 MongoDB 删除文档是 MongoDB 中的一种常见操作,用于从集合中删除文档。本文将详细介绍 MongoDB 删除文档的相关操作,包括删除文档语法格式、参数说明、删除符合条件的文档等。 一、删除文档...

    mongodb使用文档集

    MongoDB 是一个流行的开源、分布式文档数据库,常用于构建高性能、可扩展的数据存储解决方案。它以其灵活的数据模型、丰富的查询语言以及易于部署和管理的特点,成为现代Web应用程序和大数据处理的首选工具。以下是...

    mongodb操作文档.doc

    与传统的关系型数据库不同,MongoDB 不需要预先定义数据结构,而是采用文档导向的数据模型,支持BSON(二进制JSON)格式存储数据。 在 MongoDB 中,数据库、集合(相当于关系型数据库中的表)和文档(类似JSON对象...

    MongoDB快速入门笔记(六)之MongoDB的文档修改操作

    在MongoDB中,数据以文档的形式存储,文档是一组键值对,类似于JSON对象,这使得MongoDB非常适合处理半结构化和非结构化数据。 在MongoDB中,对文档的修改操作是通过`update`方法来实现的。这个方法接收四个参数: ...

    mongodb安装文档和工具类.zip

    同时,掌握MongoDB的文档结构和查询语言(MQL,MongoDB Query Language)也是必要的,这将帮助开发者编写出高效、灵活的数据操作代码。 此外,对于大型项目,了解MongoDB的集群部署、分片策略、复制集等高级特性也...

    PHP操作MONGODB详细文档.docx

    $query = new MongoDB\Driver\Query([]); $cursor = $collection->executeQuery($query); foreach ($cursor as $document) { echo json_encode($document, JSON_PRETTY_PRINT); } ``` - 更新文档: ```php $bulk = ...

    MongoDB更新文档.pdf

    MongoDB 更新文档 MongoDB 是一个流行的 NoSQL 数据库管理系统,它提供了多种方式来更新文档。本文将介绍 MongoDB 中更新文档的相关操作,包括 update() 方法、参数说明、参数和部分操作符的应用方法。 一、update...

    MongoDBQuery_C#_C#Mongo_exceptn8b_MongoDB_

    MongoDB 是一个高性能、分布式、开源的文档型数据库,它以JSON格式存储数据,适合处理大量半结构化或非结构化数据。在本项目中,我们聚焦于使用C#语言来与MongoDB进行交互,执行查询操作,并且支持将查询结果导出到...

    MongoDB_文档_查询

    本文将重点介绍在MongoDB中如何进行文档查询,包括基本查询语法、与传统RDBMS(关系型数据库管理系统)WHERE语句的比较,以及AND、OR条件的使用。 1. **查询文档** MongoDB 提供了 `find()` 方法用于查询文档。...

    SpringBoot整合MongoDB快速上手文档.zip

    MongoDB是一种流行的开源文档数据库,以其灵活性和高性能而受到广大开发者喜爱。在Java开发领域,SpringBoot框架提供了简便的方法来整合各种数据存储技术,包括MongoDB。本文档将详细介绍如何利用SpringBoot快速整合...

    SpringBoot整合MongoDB.docx

    MongoDB 是一款流行的文档型数据库,适用于处理大量结构化和半结构化的数据。SpringBoot 提供了 `spring-boot-starter-data-mongodb` 依赖,使得与 MongoDB 的交互变得简单。 首先,我们需要在 Maven 项目的 `pom....

    mongodb客户端

    3. 查询语言差异:MongoDB使用MQL(MongoDB Query Language),与SQL有显著不同。需要学习新的查询语法。 4. 处理关联数据:MySQL中的外键在MongoDB中通常通过嵌入文档或者引用实现。根据数据模型,需要决定如何处理...

    MongoDB 删除文档

    例如,如果你想要删除所有标题为 'MongoDB 教程' 的文档,可以先运行 `db.col.find({'title': 'MongoDB 教程'})` 来查看将被删除的文档。 在 MongoDB 2.6 版本及以上,`remove()` 函数的语法稍有变化,增加了一个 `...

    MongoDB基础教程

    MongoDB提供了强大的查询语言——MQL(MongoDB Query Language),支持丰富的查询表达式和聚合框架。你可以根据字段、条件进行查询,并使用正则表达式、数组过滤等高级特性。 在性能方面,MongoDB支持索引,可以...

    MongoDB in action 源码

    3. 查询语言:MongoDB的查询语言MQL(MongoDB Query Language)类似于SQL,但更适应文档数据。学习如何使用查询选择器、投影、排序、分组和聚合函数是理解MongoDB的关键。 4. 复合索引:为了提高查询性能,可以为...

    mongodb的小例子

    6. **查询语言**:MongoDB提供了强大的查询语言MQL(MongoDB Query Language)。你可以通过查询条件筛选文档,比如`{'field': 'value'}`,也可以使用`$gt`, `$lt`, `$in`等操作符进行复杂查询。 7. **聚合框架**:...

    spring-data使用mongodbTemplate对MongoDB进行读写操作

    Spring Data MongoDB是一个强大的Java库,它为开发人员提供了一种简单的方式来访问和操作MongoDB数据库。这个库是Spring Data框架的一部分,旨在简化数据访问层的实现,尤其在使用NoSQL数据库如MongoDB时。MongoDB...

Global site tag (gtag.js) - Google Analytics