MongoDB学习二--MongoDB 数据结构和查询
2015-08-18 22:28
411 查看
MongoDB数据结构与存储
MongoDB保存数据在documents中,数据是类JSON结构的key-value对。
Formally,
MongoDB documents are BSON documents.
BSON is a binary representation of JSON with
additional type information. In the documents, the value of a field can be any of the BSON data types, including other documents, arrays, and arrays of documents. For more information, see Documents.
MongoDB
stores all documents in collections.
A collection is a group of related documents that have a set of shared common indexes. Collections are analogous to a table in relational databases.
MongoDB数据查询
In
MongoDB a query targets a specific collection of documents. Queries specify criteria, or conditions, that identify the documents that MongoDB returns to the clients. A query may include a projection that
specifies the fields from the matching documents to return. You can optionally modify queries to impose limits, skips, and sort orders.
In
the following diagram, the query process specifies a query criteria and a sort modifier:
In
MongoDB, the db.collection.find() method
retrieves documents from a collection. [1] Thedb.collection.find() method
returns a cursor to
the retrieved documents.
The db.collection.findOne() method
also performs a read operation to return a single document. Internally, the db.collection.findOne() method
is the db.collection.find() method
with a limit of 1.
Select
All Documents in a Collection
Not specifying a query document to the find() is
equivalent to specifying an empty query document. Therefore the following operation is equivalent to the previous operation:
To specify equality condition, use the query document { <field>: <value> } to
select all documents that contain the <field> with
the specified <value>.
Specify Conditions Using Query Operators
A
query document can use the query operators to specify conditions in a MongoDB query.
The following example selects all documents in the inventory collection where the value of the type field is either 'food' or 'snacks':
Although you can express this query using the $or operator, use the $in operator rather than the $or operator when performing equality checks on the same field.
Specify
AND Conditions
A
compound query can specify conditions for more than one field in the collection’s documents. Implicitly, a logical AND conjunction connects the clauses of a compound query so that the query selects the documents in the collection that match all the conditions.
Specify OR Conditions
Using
the $or operator, you can specify a compound query that joins each clause with a logical OR conjunction so that the query selects the documents in the collection that match at least one condition.
In the following example, the query document selects all documents in the collection where the field qty has a value greater than ($gt) 100 or the value of the price field is less than ($lt) 9.95:
Specify AND as well as OR Conditions
With
additional clauses, you can specify precise conditions for matching documents.
In the following example, the compound query document selects all documents in the collection where the value of the type field is 'food' and either the qty has a value greater than ($gt) 100 or the value of the price field is less than ($lt) 9.95:
more infomation refer to http://docs.mongodb.org/master/tutorial/query-documents/
MongoDB保存数据在documents中,数据是类JSON结构的key-value对。
Formally,
MongoDB documents are BSON documents.
BSON is a binary representation of JSON with
additional type information. In the documents, the value of a field can be any of the BSON data types, including other documents, arrays, and arrays of documents. For more information, see Documents.
MongoDB
stores all documents in collections.
A collection is a group of related documents that have a set of shared common indexes. Collections are analogous to a table in relational databases.
MongoDB数据查询
In
MongoDB a query targets a specific collection of documents. Queries specify criteria, or conditions, that identify the documents that MongoDB returns to the clients. A query may include a projection that
specifies the fields from the matching documents to return. You can optionally modify queries to impose limits, skips, and sort orders.
In
the following diagram, the query process specifies a query criteria and a sort modifier:
In
MongoDB, the db.collection.find() method
retrieves documents from a collection. [1] Thedb.collection.find() method
returns a cursor to
the retrieved documents.
The db.collection.findOne() method
also performs a read operation to return a single document. Internally, the db.collection.findOne() method
is the db.collection.find() method
with a limit of 1.
Select
All Documents in a Collection
db.inventory.find( {} )
Not specifying a query document to the find() is
equivalent to specifying an empty query document. Therefore the following operation is equivalent to the previous operation:
db.inventory.find()
To specify equality condition, use the query document { <field>: <value> } to
select all documents that contain the <field> with
the specified <value>.
db.inventory.find( { type: "snacks" } )
Specify Conditions Using Query Operators
A
query document can use the query operators to specify conditions in a MongoDB query.
The following example selects all documents in the inventory collection where the value of the type field is either 'food' or 'snacks':
db.inventory.find( { type: { $in: [ 'food', 'snacks' ] } } )
Although you can express this query using the $or operator, use the $in operator rather than the $or operator when performing equality checks on the same field.
Specify
AND Conditions
A
compound query can specify conditions for more than one field in the collection’s documents. Implicitly, a logical AND conjunction connects the clauses of a compound query so that the query selects the documents in the collection that match all the conditions.
db.inventory.find( { type: 'food', price: { $lt: 9.95 } } )
Specify OR Conditions
Using
the $or operator, you can specify a compound query that joins each clause with a logical OR conjunction so that the query selects the documents in the collection that match at least one condition.
In the following example, the query document selects all documents in the collection where the field qty has a value greater than ($gt) 100 or the value of the price field is less than ($lt) 9.95:
db.inventory.find( { $or: [ { qty: { $gt: 100 } }, { price: { $lt: 9.95 } } ] } )
Specify AND as well as OR Conditions
With
additional clauses, you can specify precise conditions for matching documents.
In the following example, the compound query document selects all documents in the collection where the value of the type field is 'food' and either the qty has a value greater than ($gt) 100 or the value of the price field is less than ($lt) 9.95:
db.inventory.find( { type: 'food', $or: [ { qty: { $gt: 100 } }, { price: { $lt: 9.95 } } ] } )
more infomation refer to http://docs.mongodb.org/master/tutorial/query-documents/
相关文章推荐
- 散列-分离链接法(数据结构与算法分析-C语言描述)
- 数据结构(Java语言)——AVLTree简单实现
- iOS开发——OC常用的数据结构一览。
- 数据结构基本概念
- 数据结构之---C语言实现最短路径之Dijkstra(迪杰斯特拉)算法
- 浅谈数据结构-树和二叉树之间关系
- 浅谈数据结构-二叉树
- POJ 3125 Printer Queue 数据结构 队列
- 2015-8-18数据结构-动态规划-矩阵乘法次数最少
- 数据结构—排序总结
- 数据结构实验之二叉树的建立与遍历
- POJ 2259 Team Queue 数据结构 队列
- 浅谈数据结构-树
- 数据结构实验之求二叉树后序遍历和层次遍历
- 传说中的数据结构 SDUT 2556
- 数据结构上机测试4.1:二叉树的遍历与应用1--知先序和中序求后序
- c语言实现数据结构中的链式表
- c语言实现数据结构中的顺序表
- c语言实现数据结构中的哈希表
- c语言实现数据结构中的队列