import pymongo
# connection = pymongo.MongoClient(mongo_server, 27017)
username = 'davelee'
password = 'korea123'
connection = pymongo.MongoClient('mongodb://%s:%s@www.funcoding.xyz' % (username, password))
db = connection.test
db = connection["test"] # 이렇게도 가능하다.
print(db)
print(dir(db))
print(db.name)
test_collection = db.test_collection
test_collection = db["test_collection"]
test_collection
post = {"author": "Mike", "text": "My first blog post!", "tags": ["mongodb", "python", "pymongo"] }
test_collection.insert_one(post)
post = {"author": "Dave", "text": "My first blog post!", "tags": ["mongodb", "python", "pymongo"] }
post_id = test_collection.insert_one(post).inserted_id
post_id
test_collection.count()
result = test_collection.insert_many(
[
{'number': i} for i in range(10)
]
)
test_collection.count()
test_insert_collection = db.test_insert # collection 만들기가 매우 편합니다.
# 리스트, 객체 삽입 가능
test_insert_collection.insert_one({'title' : '암살', 'castings' : ['이정재', '전지현', '하정우']})
test_insert_collection.insert_one({'title' : '실미도', 'castings' : ['설경구', '안성기'],
'datetime' : {'year' : '2003', 'month' : 3,
'val' : {'a' :{'b' : 1}}}})
data = list()
data.append({'name' : 'aaron', 'age' : 20})
data.append({'name' : 'bob', 'age' : 30})
data.append({'name' : 'cathy', 'age' : 25})
data.append({'name' : 'david', 'age' : 27})
data.append({'name' : 'erick', 'age' : 28})
data.append({'name' : 'fox', 'age' : 32})
data.append({'name' : 'hmm'})
test_insert_collection.insert_many(data)
for result in test_insert_collection.find():
print(result)
data = test_insert_collection.find()
data
data_dict = list()
for dat in data:
print(type(dat))
test_collection.find_one()
test_collection.find_one( {"author":"Dave"} )
docs = test_collection.find()
for doc in docs:
print(doc)
docs = test_collection.find( {"author":"Dave"} )
for doc in docs:
print(doc)
test_collection.find({"author": "Mike"}).count()
for post in test_collection.find({"author":"Mike"}).sort("_id"):
print(post)
test_collection.find_one( {"author":"Dave"} )
test_collection.update_one(
{ "author" : "Dave" },
{ "$set" :
{ "text" : "My second blog post!" }
}
)
docs = test_collection.find( {"author":"Dave"} )
for doc in docs:
print(doc)
test_collection.update_many({"author": "Dave"}, {"$set": {"text": "My second blog post!"}})
docs = test_collection.find( {"author":"Dave"} )
for doc in docs:
print(doc)
docs = test_collection.find( {"author":"Mike"} )
for doc in docs:
print(doc)
test_collection.delete_one( {"author":"Mike"} )
docs = test_collection.find( {"author":"Mike"} )
for doc in docs:
print(doc)
test_collection.delete_many( {"author":"Mike"} )
test_collection.find( {"author":"Mike"} ).count()
from pandas import Series, DataFrame
raw_data = {'col0': [1, 2, 3, 4],
'col1': [10, 20, 30, 40],
'col2': [100, 200, 300, 400]}
data = DataFrame(raw_data)
data
data_array = data['col1'].as_matrix()
data_array.size
test_collection.find().count()
for doc in test_collection.find():
print(doc)
test_collection.delete_many( {"number": { "$gt": 5 } } )
for doc in test_collection.find():
print(doc)