下载完安装包,解压
tar -xvf mongodb-linux-x86_64-3.0.6.tgz -C /opt
cd /opt/mongodb-linux-x86_64-3.0.6/bin
创建数据库目录
mkdir -p /data/db
启动mongodb
./mongod
启动mongo时可以命令行添加参数
--port arg 端口 默认27017
--bind_ip arg 绑定ip 默认127.0.0.1
--auth 加密运行
--noauth 不加密
--fork 守护进程启动
--dbpath arg 指定数据库目录 默认 /data/db
启动客户端
命令格式为:./mongo [options][db address] [file names (ending in .js)]
./mongo #默认连接本机
创建数据库
use <数据库名>
use mydb
创建集合
db.createCollection(<集合名>)
db.createCollection("col")
插入
db.col.insert(<文档>)
db.col.insert({
name:"Tom",
age:20
})
查询
db.col.find(<查询条件>)
db.col.find({name: "Tom"})
修改
db.col.update(
<查询条件>,
<更新操作>
)
db.col.update(
{name: "Tom"},
{$set:{age: 25}}
)
删除
db.col.remove(
<查询条件>,
<是否只删除一个文档:可选>
)
db.col.remove(
{name: "Tom"}
)
聚合
db.col.aggregate(
<聚合操作>
)
db.col.insert([
{userId:1,date:"2019-11-18 14:54:28"},
{userId:1,date:"2019-11-18 14:54:28"},
{userId:1,date:"2019-11-18 14:54:28"},
{userId:2,date:"2019-11-18 14:54:28"},
{userId:3,date:"2019-11-18 14:54:28"},
{userId:2,date:"2019-11-18 14:54:28"}
])
db.col.aggregate([{$group : {_id : "$userId", count : {$sum : 1}}}])
建立数据库和集合
use qqDynamics
show dbs
db.createCollection("qqDynamics")
设计结构
文档结构如下
{
qq:QQ号
name:昵称
text:动态内容
comments:列表,评论与回复
createdDatetime:创建时间
}
其中comments为列表格式,可以嵌套该文档,实现多级的回复与评论.
但是使用嵌套会限制数据的大小不能超过16MB,也就不能用较多的评论,较多时应改用引用的方式 会增加查询次数.
设计操作语句
创建新动态
db.qqDynamics.insert({
qq: 652281589,
name: "伍慧",
text: "为什东西成为人员.的话提高具有继续为了相关责任.工程国际首页就是这么那么.",
comments: [],
createdDatetime: "2019-02-11 03:47:16"
});
评论
db.qqDynamics.update(
{"_id" : ObjectId("5e76f81abd5867c50aa550f5")},
{$push:{‘comments‘: {
qq: 611376741,
name: "梁琴",
text: "位置客户中文因为.",
comments: [],
createdDatetime: "2019-01-11 19:03:09"
} }}
)
回复评论
db.qqDynamics.update(
{"_id" : ObjectId("5e76f81abd5867c50aa550f5")},
{$push:{‘comments.0.comments‘: {
qq: 635902894,
name: "伍慧",
text: "系统品牌的是文章.",
comments: [],
createdDatetime: "2019-12-05 08:15:00"
} }}
)
回复的回复
db.qqDynamics.update(
{"_id" : ObjectId("5e76f81abd5867c50aa550f5")},
{$push:{‘comments.0.comments.0.comments‘: {
qq: 611376741,
name: "梁琴",
text: "AAAAA.",
comments: [],
createdDatetime: "2019-01-11 19:03:09"
} }}
)
使用pymongo
连接mongo数据库,进行分析,使用pyecharts
绘制图标
#导入库,并连接数据库
from pyecharts.charts import *
from pprint import pprint
import pymongo
client = pymongo.MongoClient("mongodb://localhost:27017/")
db = client["orderData"]
col = db["orderInfoCol"]
# 统计每日amount,绘制折线图
cur = col.aggregate([
{
"$group" :{
"_id":"$createdDatetime",
"amount":{"$sum":"$amount"}
}
},
{
"$sort":{"_id":1}
}
])
data = [i for i in cur]
pprint(data)
line = (
Line()
.add_xaxis([i["_id"] for i in data])
.add_yaxis("amount",[i["amount"] for i in data])
)
line.render_notebook()
#统计不同memberLevel的总amount的比例
cur = col.aggregate([
{
"$group" :{
"_id":"$customer.memberLevel",
"amount":{"$sum":"$amount"}
}
}
])
data = [i for i in cur]
pprint(data)
pie = (
Pie()
.add("amount",[(i[‘_id‘],i[‘amount‘]) for i in data])
)
pie.render_notebook()
#统计customer的总amount
cur = col.aggregate([
{
"$group" :{
"_id":"$customer.customerName",
"amount":{"$sum":"$amount"}
}
}
])
data = [i for i in cur]
pprint(data)
bar = (
Bar()
.add_xaxis([i[‘_id‘] for i in data])
.add_yaxis("amount",[i[‘amount‘] for i in data])
)
bar.render_notebook()
#统计item的总quantity
cur = col.aggregate([
{
"$unwind":"$detail"
},
{
"$group" :{
"_id":"$detail.item.itemName",
"quantity":{"$sum":"$detail.quantity"}
}
}
])
data = [i for i in cur]
pprint(data)
pie = (
Pie()
.add("count",[(i[‘_id‘],i[‘quantity‘]) for i in data])
)
pie.render_notebook()
#统计category的quantity
cur = col.aggregate([
{
"$unwind":"$detail"
},
{
"$group" :{
"_id":"$detail.item.category.category",
"quantity":{"$sum":"$detail.quantity"}
}
}
])
data = [i for i in cur]
pprint(data)
pie = (
Pie()
.add("count",[(i[‘_id‘],i[‘quantity‘]) for i in data])
)
pie.render_notebook()
#测试数据
use orderData
show dbs
db.createCollection("orderInfoCol")
db.orderInfoCol.insert({
orderID : 100001,
customer : {customerID:1001, customerName:"1001", memberLevel:1},
amount : 900,
detail : [
{item:{itemID:10001, itemName:"10001", category:{categoryID:101, category:"101"}}, quantity:10},
{item:{itemID:10002, itemName:"10002", category:{categoryID:101, category:"101"}}, quantity:20},
{item:{itemID:10003, itemName:"10003", category:{categoryID:101, category:"101"}}, quantity:30},
{item:{itemID:10004, itemName:"10004", category:{categoryID:102, category:"102"}}, quantity:40},
{item:{itemID:10005, itemName:"10005", category:{categoryID:102, category:"102"}}, quantity:50},
{item:{itemID:10006, itemName:"10006", category:{categoryID:103, category:"103"}}, quantity:50}
],
createdDatetime : "2020-01-01"
});
db.orderInfoCol.insert({
orderID : 100002,
customer : {customerID:1002, customerName:"1002", memberLevel:2},
amount : 600,
detail : [
{item:{itemID:10002, itemName:"10002", category:{categoryID:101, category:"101"}}, quantity:40},
{item:{itemID:10003, itemName:"10003", category:{categoryID:101, category:"101"}}, quantity:60},
{item:{itemID:10004, itemName:"10004", category:{categoryID:102, category:"102"}}, quantity:80},
{item:{itemID:10005, itemName:"10005", category:{categoryID:102, category:"102"}}, quantity:100}
],
createdDatetime : "2020-01-02"
});
db.orderInfoCol.insert({
orderID : 100003,
customer : {customerID:1002, customerName:"1002", memberLevel:2},
amount : 500,
detail : [
{item:{itemID:10003, itemName:"10003", category:{categoryID:101, category:"101"}}, quantity:90},
{item:{itemID:10004, itemName:"10004", category:{categoryID:102, category:"102"}}, quantity:120},
{item:{itemID:10005, itemName:"10005", category:{categoryID:102, category:"102"}}, quantity:150}
],
createdDatetime : "2020-01-03"
});
db.orderInfoCol.insert({
orderID : 100004,
customer : {customerID:1003, customerName:"1003", memberLevel:1},
amount : 300,
detail : [
{item:{itemID:10004, itemName:"10004", category:{categoryID:102, category:"102"}}, quantity:160},
{item:{itemID:10005, itemName:"10005", category:{categoryID:102, category:"102"}}, quantity:200}
],
createdDatetime : "2020-01-04"
});
db.orderInfoCol.insert({
orderID : 100005,
customer : {customerID:1003, customerName:"1003", memberLevel:1},
amount : 400,
detail : [
{item:{itemID:10005, itemName:"10005", category:{categoryID:102, category:"102"}}, quantity:900}
],
createdDatetime : "2020-01-05"
});
db.orderInfoCol.insert({
orderID : 100006,
customer : {customerID:1003, customerName:"1003", memberLevel:1},
amount : 200,
detail : [
{item:{itemID:10005, itemName:"10005", category:{categoryID:102, category:"102"}}, quantity:900}
],
createdDatetime : "2020-01-06"
});
原文:https://www.cnblogs.com/lzyuid/p/12558492.html