很好的实现了面对对象的思想,,在Mongo DB中 每一条记录都是一个Document对象。
安装 MongoDB
sudo apt-get install mongo
在终端输入”mongo”进入数据库:
mongo
Import Example Dataset
-
1 Retrieve the restaurants data
- Retrieve the dataset from (here)[ https://raw.githubusercontent.com/mongodb/docs-assets/primer-dataset/dataset.json] and save to a file named primer-dataset.json. 2 Import data into the collection
-
In the system shell or command prompt, use
mongoimport
to insert the documents into the restaurants collection in the test database.
mongoimport --db test --collection restaurants --drop --file primer-dataset.json
The mongoimport
connects to a mongod instance running on localhost on port number 27017
.
Python Driver (PyMongo)
1 Install PyMongo
pip install pymongo
2 Import pymongo
from pymongo import MongoClient
3 Create a Connection
client = MongoClient()
If you do not specify any arguments to MongoClient
, then MongoClient defaults to the MongoDB instance that runs on the localhost
interface on port 27017
.
4 Access Database Objects
to assign the local variable db to the database named primer, you can use attribute access, as in the following:
db = client.primer
You can also access databases using dictionary-style access, which removes Python-specific naming restrictions, as in the following:
db = client['primer']
5 Access Collection Objects
You can access collection objects directly using dictionary-style or attribute access from a Database object, as in the following examples:
db.dataset
db['dataset']
You may also assign the collection object to a variable for use elsewhere, as in the following examples:
coll = db.dataset
coll = db['dataset']
Insert Data with PyMongo
1 Overview
You can use the insert_one()
method and the insert_many()
method to add documents to a collection
in MongoDB. If you attempt to add documents to a collection that does not exist, MongoDB will create the collection for you.
2 Insert a Document
Insert a document into a collection named restaurants. The operation will create the collection if the collection does not currently exist.
from datetime import datetime
result = db.restaurants.insert_one(
{
"address": {
"street": "2 Avenue",
"zipcode": "10075",
"building": "1480",
"coord": [-73.9557413, 40.7720266]
},
"borough": "Manhattan",
"cuisine": "Italian",
"grades": [
{
"date": datetime.strptime("2014-10-01", "%Y-%m-%d"),
"grade": "A",
"score": 11
},
{
"date": datetime.strptime("2014-01-16", "%Y-%m-%d"),
"grade": "B",
"score": 17
}
],
"name": "Vella",
"restaurant_id": "41704620"
}
)
The operation returns an InsertOneResult object, which includes an attribute inserted_id that contains the _id of the inserted document. Access the inserted_id attribute:
result.inserted_id
Find or Query Data with PyMongo
1 Overview
You can use the find() method to issue a query to retrieve data from a collection in MongoDB. All queries in MongoDB have the scope of a single collection.
2 Query for All Documents in a Collection
cursor = db.restaurants.find()
Iterate the cursor and print the documents:
for document in cursor:
print(document)
3 Specify Equality Conditions
The query condition for an equality match on a field has the following form:
{ <field1>: <value1>, <field2>: <value2>, ... }
4 Query by a Top Level Field
The following operation finds documents whose borough field equals “Manhattan”:
cursor = db.restaurants.find({"borough": "Manhattan"})
5 Query by a Field in an Embedded Document
cursor = db.restaurants.find({"address.zipcode": "10075"})
6 Specify Conditions with Operators
MongoDB provides operators to specify query conditions, such as comparison operators. Although there are some exceptions, such as the
orand
and conditional operators, query conditions using operators generally have the following form:
{ <field1>: { <operator1>: <value1> } }
Comparison
Name | Description |
---|---|
$eq | Matches values that are equal to a specified value. |
$gt | Matches values that are greater than a specified value. |
$gte | Matches values that are greater than or equal to a specified value. |
$lt | Matches values that are less than a specified value. |
$lte | Matches values that are less than or equal to a specified value. |
$ne | Matches all values that are not equal to a specified value. |
$in | Matches any of the values specified in an array. |
$nin Matches none of the values specified in an array.
Logical
Name | Description |
---|---|
$or | Joins query clauses with a logical OR returns all documents that match the conditions of either clause. |
$and | Joins query clauses with a logical AND returns all documents that match the conditions of both clauses. |
$not | Inverts the effect of a query expression and returns documents that do not match the query expression. |
$nor | Joins query clauses with a logical NOR returns all documents that fail to match both clauses. |
Element
Name | Description |
---|---|
$exists | Matches documents that have the specified field. |
$type | Selects documents if a field is of the specified type. |
7 Sort Query Results
To specify an order for the result set, append the sort() method to the query. Pass to sort() method a document which contains the field(s) to sort by and the corresponding sort type, e.g. pymongo.ASCENDING for ascending and pymongo.DESCENDING for descending.
import pymongo
cursor = db.restaurants.find().sort([
("borough", pymongo.ASCENDING),
("address.zipcode", pymongo.DESCENDING)
])
Update Data with PyMongo
1 Overview
You can use the update_one() and the update_many() methods to update documents of a collection. The update_one() method updates a single document. Use update_many() to update all documents that match the criteria. The methods accept the following parameters:
2 Update Specific Fields
To change a field value, MongoDB provides update operators, such as
settomodifyvalues.Someupdateoperators,suchas
set, will create the field if the field does not exist.
Update Top-Level Fields
result = db.restaurants.update_one(
{"name": "Juni"},
{
"$set": {
"cuisine": "American (New)"
},
"$currentDate": {"lastModified": True}
}
)
To see the number of documents that matched the filter condition, access the matched_count attribute of the returned UpdateResult object:
result.matched_count
To see the number of documents modified by the update operation, access the modified_count attribute of the returned UpdateResult object:
result.modified_count
3 Replace a Document
To replace the entire document except for the _id field, pass an entirely new document as the second argument to the update() method
After the following update, the modified document will only contain the _id field, name field, the address field. i.e. the document will not contain the restaurant_id, cuisine, grades, and the borough fields.
result = db.restaurants.replace_one(
{"restaurant_id": "41704620"},
{
"name": "Vella 2",
"address": {
"coord": [-73.9557413, 40.7720266],
"building": "1480",
"street": "2 Avenue",
"zipcode": "10075"
}
}
)
Remove Data with PyMongo
1 Overview
You can use the delete_one() method and the delete_many() method to remove documents from a collection. The method takes a conditions document that determines the documents to remove.
2 Remove All Documents That Match a Condition
result = db.restaurants.delete_many({"borough": "Manhattan"})
To see the number of documents deleted, access the deleted_count attribute of the returned DeleteResult object.
result.deleted_count
3 Remove All Documents
result = db.restaurants.delete_many({})
4 Drop a Collection
db.restaurants.drop()
Data Aggregation with PyMongo
1 Overview
MongoDB can perform aggregation operations, such as grouping by a specified key and evaluating a total or a count for each distinct group.
Use the aggregate()
method to perform a stage-based aggregation. The aggregate() method accepts as its argument an array of stages, where each stage
, processed sequentially, describes a data processing step.
db.collection.aggregate([<stage1>, <stage2>, ...])
2 Group Documents by a Field and Calculate Count
Use the $group
stage to group by a specified key. In the $group
stage, specify the group by key in the _id field. $group
accesses fields by the field path, which is the field name prefixed by a dollar sign $
. The $group
stage can use accumulators to perform calculations for each group. The following example groups the documents in the restaurants collection by the borough field and uses the $sum
accumulator to count the documents for each group.
cursor = db.restaurants.aggregate(
[
{"$group": {"_id": "$borough", "count": {"$sum": 1}}}
]
)
3 Filter and Group Documents
Use the $match
stage to filter documents. $match
uses the MongoDB query syntax. The following pipeline uses $match
to query the restaurants collection for documents with borough equal to “Queens” and cuisine equal to Brazilian. Then the $group
stage groups the matching documents by the address.zipcode field and uses the $sum
accumulator to calculate the count.
cursor = db.restaurants.aggregate(
[
{"$match": {"borough": "Queens", "cuisine": "Brazilian"}},
{"$group": {"_id": "$address.zipcode", "count": {"$sum": 1}}}
]
)
Indexes with PyMongo
1 Overview
Indexes can support the efficient execution of queries. MongoDB automatically creates an index on the _id field upon the creation of a collection.
Use the create_index() method to create an index on a collection. Indexes can support the efficient execution of queries. MongoDB automatically creates an index on the _id field upon the creation of a collection.
[ ( <field1>: <type1> ), ... ]
- For an ascending index, specify pymongo.ASCENDING for .
- For a descending index, specify pymongo.DESCENDING for .
2 Create a Single-Field Index
Create an ascending index on the “cuisine” field of the restaurants collection.
import pymongo
db.restaurants.create_index([("cuisine", pymongo.ASCENDING)])
3 Create a compound index
import pymongo
db.restaurants.create_index([
("cuisine", pymongo.ASCENDING),
("address.zipcode", pymongo.DESCENDING)
])