start_mongo
db.version()
show dbs
use training
db.createCollection("mycollection")
show collections
db.mycollection.insert({"color":"blue","example":"sky"})
db.mycollection.count()
db.mycollection.find()
exit
本部分采用的collection名字为languages
首先插入一些数据:
db.languages.insert({"name":"java","type":"object oriented"})
db.languages.insert({"name":"python","type":"general purpose"})
db.languages.insert({"name":"scala","type":"functional"})
db.languages.insert({"name":"c","type":"procedural"})
db.languages.insert({"name":"c++","type":"object oriented"})
read
db.languages.findOne()
db.languages.find()
db.languages.find().limit(3)
db.languages.find({"name":"python"})
db.languages.find({},{"name":1})
db.languages.find({},{"name":0})
db.languages.find({"type":"object oriented"},{"name":1})
update
通用语法:
db.collection.updateMany({what documents to find},{$set:{what fields to set}})
举例:
db.languages.updateMany({},{$set:{"description":"programming language"}})
db.languages.updateMany({"name":"python"},{$set:{"creator":"Guido van Rossum"}})
delete
db.languages.remove({"name":"scala"})
db.languages.remove({"type":"object oriented"})
db.languages.remove({})
总结:
CRUD四种操作,基本模式使用{key:value}
来进行筛选要改动的doc
其中update稍微复杂一点,需要先找到,然后再更改,改的部分,需要使用$set
标识出来。
bigdata是学习本部分创建的Collection,通过如下循环创建了20万个doc:
use training
for (i=1;i<=200000;i++){print(i);db.bigdata.insert({"account_no":i,"balance":Math.round(Math.random()*1000000)})}
db.bigdata.createIndex({"account_no":1})
db.bigdata.getIndexes()
db.bigdata.dropIndex({"account_no":1})
marks是学习本部分创建的Collection,具体数据内容如下:
use training
db.marks.insert({"name":"Ramesh","subject":"maths","marks":87})
db.marks.insert({"name":"Ramesh","subject":"english","marks":59})
db.marks.insert({"name":"Ramesh","subject":"science","marks":77})
db.marks.insert({"name":"Rav","subject":"maths","marks":62})
db.marks.insert({"name":"Rav","subject":"english","marks":83})
db.marks.insert({"name":"Rav","subject":"science","marks":71})
db.marks.insert({"name":"Alison","subject":"maths","marks":84})
db.marks.insert({"name":"Alison","subject":"english","marks":82})
db.marks.insert({"name":"Alison","subject":"science","marks":86})
db.marks.insert({"name":"Steve","subject":"maths","marks":81})
db.marks.insert({"name":"Steve","subject":"english","marks":89})
db.marks.insert({"name":"Steve","subject":"science","marks":77})
db.marks.insert({"name":"Jan","subject":"english","marks":0,"reason":"absent"})
db.marks.aggregate([{"$limit":2}])
db.marks.aggregate([{"$sort":{"marks":1}}])
{"marks":-1}
那么将会按照降序顺序输出。db.marks.aggregate([ {"$sort":{"marks":-1}}, {"$limit":2} ])
[]
中按照顺序排开,两种operator中用逗号隔开。group
经常和 sum
,avg
,min
,max
等配合使用db.marks.aggregate([
{
"$group":{
"_id":"$subject",
"average":{"$avg":"$marks"}
}
}
])
上式与如下sql相同含义:select subject, avg(marks) from marks
group by subject;
group中的tag,_id
不能改最后,所有如上的操作,都可以像pipeline一样,在[]中按照逗号排开。
redundancy
availibility
fault
sharding:
连接数据库
from pymongo import MongoClient
user = 'root'
password = 'MjQwOTgtcnNhbm5h' # 改为自己的password
host='localhost'
#create the connection url
connecturl = "mongodb://{}:{}@{}:27017/?authSource=admin".format(user,password,host)
# connect to mongodb server
print("Connecting to mongodb server")
connection = MongoClient(connecturl)
# get database list
print("Getting list of databases")
dbs = connection.list_database_names()
# print the database names
for db in dbs:
print(db)
print("Closing the connection to the mongodb server")
# close the server connecton
connection.close()
操作文件
在连接好数据库之后,可以与数据库交互:
connection = MongoClient(connecturl)
# select the 'training' database
db = connection.training
# select the 'python' collection
collection = db.python
# create a sample document
doc = {"lab":"Accessing mongodb using python", "Subject":"No SQL Databases"}
# insert a sample document
print("Inserting a document into collection.")
db.collection.insert(doc)
# query for all documents in 'training' database and 'python' collection
docs = db.collection.find()
print("Printing the documents in the collection.")
for document in docs:
print(document)