pymongoの使用

5183 ワード

1.リンクデータベース(database)とクラスタリング(collection)
import pymongo

#    MongoDB,          27017   
client = pymongo.MongoClient('localhost', 27017)

#            
test = client['test_db']

#         
test = client.test_db

#     student_info     
student_info = test['student_info']

Mongodbのデータベース(database)はSQLのdatabaseと同じで、Excelファイルとして理解できます.
Mongodbのクラスタリング(collection)は、SQLのテーブル(table)に相当し、Excelファイルのsheetとして理解できます.
2.データの挿入insert_one()文を使用してデータを挿入し、データを辞書形式にパッケージします.
 test.student_info.insert_one({'name':'  '})

反復で複数のデータを挿入するには、次の手順に従います.
student_infos = [
    {'name':'  ','age':12,'city':'  ','hobby':['  ','  ']},
    {'name':'  ','age':11,'city':'  ','hobby':['  ']},
    {'name':'  ','age':9,'city':'  ','hobby':['  ','  ','  ']},
    {'name':'  ','age':12,'city':'  ','hobby':['  ']},
    {'name':'  ','age':8,'city':'  ','hobby':['  ']}
    ]

for student in student_infos:
    student_info.insert_one(
        {
            'name':student['name'],
            'city':student['city'],
            'age':student['age'],
            'hobby':student['hobby']
            }
        )
insert_many()文で複数のレコードを挿入します.
test.student_info.\
    insert_many(
        [
            {'name':' x', 'age': 10},
            {'name':' y', 'age': 11},
            {'name':' z', 'age': 12}
        ]
        )

3.照会操作
3.1すべての集計名の表示
>>> test.collection_names()
['student_info']

3.2集計の最初のレコードの表示
>>> test.student_info.find_one()
{
    '_id': ObjectId('5a1cd216f1a19d178cc7f6c8'), 
    'city': '  ', 
    'name': '  ', 
    'age': 12, 
    'hobby': ['  ', '  ']
}

3.3条件クエリー
#    age   12    
r = test.student_info.\
    find_one(
        {'age':12}
        )
print(r)

#   
{
    'age': 12, 
    'hobby': ['  ', '  '], 
    'city': '  ', 
    '_id': ObjectId('5a1cd216f1a19d178cc7f6c8'), 
    'name': '  '
}

注意:検索条件を満たすレコードは複数あり、find_one()文には1つしか表示されません.find()文を使用して、すべての一致条件のレコードをクエリーします.
r = test.student_info.\
    find(
        {'age':12}
        )

3.4集計のレコード統計の表示
#        
>>> test.student_info.find().count()
5
#             
>>> test.student_info.distinct('city')
['  ', '  ', '  ', '  ', '  ']

3.5集計クエリー結果のソート
#      
r = test.student_info.\
    find().\
    sort("age")
#   
r = test.student_info.\
    find().\
    sort("age", pymongo.ASCENDING)
#   
r = test.student_info.\
    find().\
    sort("age", pymongo.DESCENDING)
#     
#     age   ,age       name   
r = test.student_info.\
    find().\
    sort(
        [
            ("age",pymongo.ASCENDING),
            ("name",pymongo.DESCENDING)
        ]
        )

4.修正操作
4.1レコードの変更
#    age = 12    
#           age   100
test.student_info.\
    update_one(
        {'age': 12},
        {
        '$set':{'age': 100}
        }
    )

4.2複数レコードの変更
test.student_info.\
    update_many(
        {'age': 100},
        {
        '$set':{'age': 1}
        }
    )

4.3全記録の修正
#  _id   ,      ,      'age'    :100
for i in test.student_info.find():
    test.student_info.\
    update_one(
        {'_id':i['_id']}, 
        {'$set': 
            {'age': 100}
        }
        )

5.置換操作
レコードを置換するには、次の手順に従います.
#    city    '   '   ,    'hometown':'  '
#   :           ,       'city'   
test.student_info.replace_one(
    {'city':'  '},
    {'hometown':'  '}
    )

r = test.student_info.find({'hometown':'  '})
for i in r:
    print(i)

#   
{
    '_id': ObjectId('571c901b13d5942564a5a23e'), 
    'hometown': '  '
}


6.削除操作
6.1レコードの削除
#       name    '  '    
test.student_info.delete_one({'name':'  '})

6.2複数レコードの削除
#      age   12    
test.student_info.delete_many({'age':12})