您的位置:首页 > 理论基础 > 数据结构算法

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

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/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: