MongoDB学习

本文详细介绍了MongoDB的安装过程、使用Python的PyMongo库进行数据导入、查询、更新和删除的操作,包括数据聚合和创建索引。通过实例展示了如何与MongoDB交互,如插入文档、查询特定条件的数据、更新字段和删除文档。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

很好的实现了面对对象的思想,,在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

NameDescription
$eqMatches values that are equal to a specified value.
$gtMatches values that are greater than a specified value.
$gteMatches values that are greater than or equal to a specified value.
$ltMatches values that are less than a specified value.
$lteMatches values that are less than or equal to a specified value.
$neMatches all values that are not equal to a specified value.
$inMatches any of the values specified in an array.

$nin Matches none of the values specified in an array.

Logical

NameDescription
$orJoins query clauses with a logical OR returns all documents that match the conditions of either clause.
$andJoins query clauses with a logical AND returns all documents that match the conditions of both clauses.
$notInverts the effect of a query expression and returns documents that do not match the query expression.
$norJoins query clauses with a logical NOR returns all documents that fail to match both clauses.

Element

NameDescription
$existsMatches documents that have the specified field.
$typeSelects 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)
])
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值