码迷,mamicode.com
首页 > 数据库 > 详细

python mongodb操作大全

时间:2018-04-03 22:04:19      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:lex   group   names   其他   auth   www   size   主键   修改   

数据库

use db1 #有则切换,无则新增


show dbs #查看所有
db #当前


db.dropDatabase()

集合:
增:
db.user
db.user.info
db.user.auth

查看
show collections
show tables


db.user.info.drop()

文档:

db.user.insert({"_id":1,"name":"egon"})
user0={
"name":"egon",
"age":10,
‘hobbies‘:[‘music‘,‘read‘,‘dancing‘],
‘addr‘:{
‘country‘:‘China‘,
‘city‘:‘BJ‘
}
}

db.user.insert(user0)
db.user.insertMany([user1,user2,user3,user4,user5])

db.t1.insert({"_id":1,"a":1,"b":2,"c":3})
#有相同的_id则覆盖,无相同的_id则新增,必须指定_id
db.t1.save({"_id":1,"z":6})
db.t1.save({"_id":2,"z":6})
db.t1.save({"z":6})

     save与insert的区别:
   若新增的数据中存在主键 ,insert() 会提示错误,而save() 则更改原来的内容为新内容。
   如:
     已存在数据: {_id : 1, " name " : " n1 " },再次进行插入操作时,
     insert({_id : 1, " name " : " n2 " }) 会报主键重复的错误提示
     save({ _id : 1, " name " : " n2 " }) 会把 n1 修改为 n2 。
     相同点:
     若新增的数据中没有主键时,会增加一条记录。
     已存在数据: { _id : 1, " name " : " n1 " },再次进行插入操作时,
     insert({ " name " : " n2 " }) 插入的数据因为没有主键,所以会增加一条数据
     save({ " name " : " n2 " }) 增加一条数据。

比较运算:=,!=,>,<,>=,<=
#1、select * from db1.user where id = 3
db.user.find({"_id":3})

#2、select * from db1.user where id != 3
db.user.find({"_id":{"$ne":3}})

#3、select * from db1.user where id > 3
db.user.find({"_id":{"$gt":3}})

#4、select * from db1.user where age < 3
db.user.find({"age":{"$lt":3}})

#5、select * from db1.user where id >= 3
db.user.find({"_id":{"$gte":3}})

#6、select * from db1.user where id <= 3
db.user.find({"_id":{"$lte":3}})


#逻辑运算:$and,$or,$not
#1 select * from db1.user where id >=3 and id <=4;
db.user.find({"_id":{"$gte":3,"$lte":4}})

#2 select * from db1.user where id >=3 and id <=4 and age >=40;
db.user.find({
"_id":{"$gte":3,"$lte":4},
"age":{"$gte":40}
})

db.user.find({"$and":[
{"_id":{"$gte":3,"$lte":4}},
{"age":{"$gte":40}}
]})


#3 select * from db1.user where id >=0 and id <=1 or id >=4 or name = "yuanhao";
db.user.find({"$or":[
{"_id":{"$lte":1,"$gte":0}},
{"_id":{"$gte":4}},
{"name":"yuanhao"}
]})

#4 select * from db1.user where id % 2 = 1;
db.user.find({"_id":{"$mod":[2,1]}})


db.user.find({
"_id":{"$not":{"$mod":[2,1]}}
})

#成员运算:$in,$nin
db.user.find({"name":{"$in":["alex","egon"]}})
db.user.find({"name":{"$nin":["alex","egon"]}})


#正则匹配
select * from db1.user where name regexp "^jin.*?(g|n)$";
db.user.find({
"name":/^jin.*?(g|n)$/i
})


#查看指定字段
select name,age from db1.user where name regexp "^jin.*?(g|n)$";
db.user.find({
"name":/^jin.*?(g|n)$/i
},
{
"_id":0,
"name":1,
"age":1
}
)

#查询数组相关
db.user.find({
"hobbies":"dancing"
})

db.user.find({
"hobbies":{"$all":["dancing","tea"]}
})

db.user.find({
"hobbies.2":"dancing"
})

db.user.find(
{},
{
"_id":0,
"name":0,
"age":0,
"addr":0,
"hobbies":{"$slice":[1,2]},
}
)


db.user.find(
{},
{
"_id":0,
"name":0,
"age":0,
"addr":0,
"hobbies":{"$slice":2},
}
)


db.user.find(
{
"addr.country":"China"
}
)

db.user.find().sort({"_id":1,"age":-1})


db.user.find().limit(2).skip(0)
db.user.find().limit(2).skip(2)
db.user.find().limit(2).skip(4)
db.user.find().distinct()


一 语法:
db.table.update(
条件,
修改字段,
其他参数
)

update db1.t1 set id=10 where name="egon";

db.table.update(
{},
{"age":11},
{
"multi":true,
"upsert":true
}
)

1、update db1.user set age=23,name="武大郎" where name="wupeiqi";
#覆盖式
db.user.update(
{"name":"wupeiqi"},
{"age":23,"name":"武大郎"}
)
#局部修改:$set
db.user.update(
{"name":"alex"},
{"$set":{"age":73,"name":"潘金莲-alex"}}
)

#改多条
db.user.update(
{"_id":{"$gte":1,"$lte":2}},
{"$set":{"age":53,}},
{"multi":true}
)
#有则修改,无则添加
db.user.update(
{"name":"EGON"},
{"$set":{"name":"EGON","age":28,}},
{"multi":true,"upsert":true}
)

#修改嵌套文档
db.user.update(
{"name":"潘金莲-alex"},
{"$set":{"addr.country":"Japan"}}
)

#修改数组
db.user.update(
{"name":"潘金莲-alex"},
{"$set":{"hobbies.1":"Piao"}}
)

#删除字段
db.user.update(
{"name":"潘金莲-alex"},
{"$unset":{"hobbies":""}}
)

2、$inc
db.user.update(
{},
{"$inc":{"age":1}},
{"multi":true}
)

db.user.update(
{},
{"$inc":{"age":-10}},
{"multi":true}
)

3、$push, $pop $pull
db.user.update(
{"name":"yuanhao"},
{"$push":{"hobbies":"tangtou"}},
{"multi":true}
)

db.user.update(
{"name":"yuanhao"},
{"$push":{"hobbies":{"$each":["纹身","抽烟"]}}},
{"multi":true}
)

#从头删-1,从尾删1
db.user.update(
{"name":"yuanhao"},
{"$pop":{"hobbies":-1}},
{"multi":true}
)

db.user.update(
{"name":"yuanhao"},
{"$pop":{"hobbies":1}},
{"multi":true}
)

#按条件删
db.user.update(
{"name":"yuanhao"},
{"$pull":{"hobbies":"纹身"}},
{"multi":true}
)

#3、$addToSet
db.t3.insert({"urls":[]})
db.t3.update(
{},
{"$addToSet":{"urls":{"$each":[
"http://www.baidu.com",
"http://www.baidu.com",
"http://www.baidu.com",
"http://www.baidu.com",
"http://www.baidu.com"
]}}},
{"multi":true}
)



db.user.deleteOne({"_id":{"$gte":3}})
db.user.deleteMany({"_id":{"$gte":3}})
db.user.deleteMany({})


聚合
一:$match
例:
select post from db1.emp where age > 20 group by post having avg(salary) > 10000;

#$match
#1、select * from db1.emp where age > 20
db.emp.aggregate({"$match":{"age":{"$gt":20}}})

#$group
#2、select post from db1.emp where age > 20 group by post;
db.emp.aggregate(
{"$match":{"age":{"$gt":20}}},
{"$group":{"_id":"$post"}}
)

#3、select post,avg(salary) as avg_salary from db1.emp where age > 20 group by post;
db.emp.aggregate(
{"$match":{"age":{"$gt":20}}},
{"$group":{"_id":"$post","avg_salary":{"$avg":"$salary"}}}
)

#select post from db1.emp where age > 20 group by post having avg(salary) > 10000;
db.emp.aggregate(
{"$match":{"age":{"$gt":20}}},
{"$group":{"_id":"$post","avg_salary":{"$avg":"$salary"}}},
{"$match":{"avg_salary":{"$gt":10000}}}
)

二: 投射
       {"$project":{"要保留的字段名":1,"要去掉的字段名":0,"新增的字段名":"表达式"}}
例1:
db.emp.aggregate(
{"$project":{"_id":0,"name":1,"post":1,"annual_salary":{"$multiply":[12,"$salary"]}}},
{"$group":{"_id":"$post","平均年薪":{"$avg":"$annual_salary"}}},
{"$match":{"平均年薪":{"$gt":1000000}}},
{"$project":{"部门名":"$_id","平均年薪":1,"_id":0}}
)

例2:
db.emp.aggregate(
{"$project":{"_id":0,"name":1,"hire_period":{"$subtract":[new Date(),"$hire_date"]}}}
)


db.emp.aggregate(
{"$project":{"_id":0,"name":1,"hire_year":{"$year":"$hire_date"}}}
)

db.emp.aggregate(
{"$project":{"_id":0,"name":1,"hire_period":{"$subtract":[{"$year":new Date()},{"$year":"$hire_date"}]}}}
)

例3:
db.emp.aggregate(
{"$project":{"_id":0,"new_name":{"$toUpper":"$name"},}}
)

db.emp.aggregate(
{"$match":{"name":{"$ne":"egon"}}},
{"$project":{"_id":0,"new_name":{"$concat":["$name","_SB"]},}}
)

db.emp.aggregate(
{"$match":{"name":{"$ne":"egon"}}},
{"$project":{"_id":0,"new_name":{"$substr":["$name",0,3]},}}
)

三:{"$group":{"_id":分组字段,"新的字段名":聚合操作符}}

#select post,max,min,sum,avg,count,group_concat from db1.emp group by post;

db.emp.aggregate(
{"$group":{
"_id":"$post",
"max_age":{"$max":"$age"},
"min_id":{"$min":"$_id"},
"avg_salary":{"$avg":"$salary"},
"sum_salary":{"$sum":"$salary"},
"count":{"$sum":1},
"names":{"$push":"$name"}
}
}
)


四:排序:$sort、限制:$limit、跳过:$skip
db.emp.aggregate(
{"$match":{"name":{"$ne":"egon"}}},
{"$project":{"_id":1,"new_name":{"$substr":["$name",0,3]},"age":1}},
{"$sort":{"age":1,"_id":-1}},
{"$skip":5},
{"$limit":5}
)


# 补充
db.emp.aggregate({"$sample":{"size":3}}) #随机选取3个文档

python mongodb操作大全

标签:lex   group   names   其他   auth   www   size   主键   修改   

原文地址:https://www.cnblogs.com/VseYoung/p/MongoDB.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!