微信搜索superit|邀请体验:大数据, 数据管理、OLAP分析与可视化平台 | 赞助作者:赞助作者

python mongodb 操作

python aide_941 24℃

#1.查询身高小于180的文档
print ‘————-身高小于180:’
print type(collection_set01.find({‘high’:{‘$lt’:180}})) #<class ‘pymongo.cursor.Cursor’>
for r in collection_set01.find({‘high’:{‘$lt’:180}}):
print r
print type(collection_set01.find_one({‘high’:{‘$lt’:180}})) #<type ‘dict’>
print ‘use find_one:’,collection_set01.find_one({‘high’:{‘$lt’:180}})[‘high’]
print ‘use find_one:’,collection_set01.find_one({‘high’:{‘$lt’:180}})

#2.查询特定键(select key1,key2 from table;)
print ‘————-查询特定键——–‘
print ‘————-查询身高大于170,并只列出_id,high和age字段(使用列表形式_id默认打印出来,可以使用{}忽视_id):’
for r in collection_set01.find({‘high’:{‘$gt’:170}},projection=[‘high’,’age’]):print r
print ‘\n’
print ‘————–skip参数用法’
for r in collection_set01.find({‘high’:{‘$gt’:170}},[‘high’,’age’],skip=1):print r #skip=1跳过第一个匹配到的文档
for r in collection_set01.find({‘high’:{‘$gt’:170}},[‘high’,’age’]).skip(1):print r #skip=1跳过第一个匹配到的文档
print ‘\n’
print ‘————–limit参数用法’
for r in collection_set01.find({‘high’:{‘$gt’:170}},[‘high’,’age’],limit=1):print r #limit=2限制显示2条文档
print ‘\n’
print ‘————–用{}描述特定键’
for r in collection_set01.find({‘high’:{‘$gt’:170}},{‘high’:1,’age’:1,’_id’:False}):print r

print ‘———————多条件查询’
print collection_set01.find_one({‘high’:{‘$gt’:10},’age’:{‘$lt’:26,’$gt’:10}})

#3.$in
print ‘—————-IN’
for r in collection_set01.find({“age”:{“$in”:[23, 26, 32]}}): print r # select * from users where age in (23, 26, 32)
#for u in db.users.find({“age”:{“$nin”:(23, 26, 32)}}): print u # select * from users where age not in (23, 26, 32)

#4.count统计数目
print ‘—————-count’
print collection_set01.find({“age”:{“$gt”:20}}).count() # select count(*) from set01 where age > 10

#5.$or
print ‘—————-条件或’
print ‘大于等于29或者小于23’
for r in collection_set01.find({“$or”:[{“age”:{“$lte”:23}}, {“age”:{“$gte”:29}}]}): print r

#6.$exists,是否存在字段
print ‘————exists’
for r in collection_set01.find({‘age’:{‘$exists’:True}}):print ‘age exists’,r # select * from 集合名 where exists 键1
for r in collection_set01.find({‘age’:{‘$exists’:False}}):print ‘age not exists’,r # select * from 集合名 where not exists 键1

#7.正则表达式查询
print ‘正则表达式查询’
#method 1
for r in collection_set01.find({‘name’:{‘$regex’:r’.*wei.*’}}):print r #找出name字段中包含wei的文档
#method 2
import re
Regex = re.compile(r’.*zhang.*’,re.IGNORECASE)
for r in collection_set01.find({‘name’:Regex}):print r #找出name字段中包含wei的文档

#8.sort排序

print ‘——————–使用sort排序(文档中没有排序的字段也会打印出来,表示最小)’
#pymongo.ASCENDING 1
#pymongo.DESCENDING -1
#sort([[],()]),[],()均可
print ‘————–age 升序’
for r in collection_set01.find().sort([[“age”,pymongo.ASCENDING]]):print r
print ‘————–age 降序’
for r in collection_set01.find().sort([(“age”,-1)]):print r
print ‘————–age升序,high升序’
for r in collection_set01.find().sort(((“age”,pymongo.ASCENDING),(“high”,pymongo.ASCENDING))):print r
print ‘————–age升序,high降序’
for r in collection_set01.find(sort=[(“age”,pymongo.ASCENDING),(“high”,pymongo.ASCENDING)]):print r

#9.$all判断数组属性是否包含全部条件,注意与$in区别

print ‘————-$all’
for r in collection_set01.find({‘list’:{‘$all’:[2,3,4]}}):print r
print ‘————-$in’
for r in collection_set01.find({‘list’:{‘$in’:[2,3,4]}}):print r

#10.$size匹配数组属性元素数量
print ‘————-$size’
print ‘————-size=3’
for r in collection_set01.find({‘list’:{‘$size’:3}}):print r
print ‘————-size=7’
for r in collection_set01.find({‘list’:{‘$size’:7}}):print r

#11.$unset和$set相反表示移除文档属性
print ‘——————-$unset’
print ‘—before’
for r in collection_set01.find({‘name’:’weijian’}):print r
collection_set01.update({‘name’:’weijian’},{‘$unset’:{‘age’:1}})
print ‘—after’
for r in collection_set01.find({‘name’:’weijian’}):print r

#输出结果
————-查询测试———–
————-身高小于180:
<class ‘pymongo.cursor.Cursor’>
{u’high’: 173, u’comment’: u’use update_many’, u’age’: 26, u’_id’: 2, u’name’: u’zhang’}
{u’high’: 158, u’age’: 30, u’_id’: 4, u’name’: u’weijian’}
{u’high’: 179, u’comment’: u’use update_many’, u’age’: 22, u’_id’: 5, u’name’: u’zhangjian’}
{u’high’: 176, u’comment’: u’use update_many’, u’age’: -27, u’_id’: 0, u’name’: u’zzzzz’}
<type ‘dict’>
use find_one: 173
use find_one: {u’high’: 173, u’comment’: u’use update_many’, u’age’: 26, u’_id’: 2, u’name’: u’zhang’}
————-查询特定键——–
————-查询身高大于170,并只列出_id,high和age字段(使用列表形式_id默认打印出来,可以使用{}忽视_id):
{u’high’: 173, u’age’: 26, u’_id’: 2}
{u’high’: 180, u’age’: 29, u’_id’: 3}
{u’high’: 179, u’age’: 22, u’_id’: 5}
{u’high’: 176, u’age’: -27, u’_id’: 0}

————–skip参数用法
{u’high’: 180, u’age’: 29, u’_id’: 3}
{u’high’: 179, u’age’: 22, u’_id’: 5}
{u’high’: 176, u’age’: -27, u’_id’: 0}
{u’high’: 180, u’age’: 29, u’_id’: 3}
{u’high’: 179, u’age’: 22, u’_id’: 5}
{u’high’: 176, u’age’: -27, u’_id’: 0}

————–limit参数用法
{u’high’: 173, u’age’: 26, u’_id’: 2}

————–用{}描述特定键
{u’high’: 173, u’age’: 26}
{u’high’: 180, u’age’: 29}
{u’high’: 179, u’age’: 22}
{u’high’: 176, u’age’: -27}
———————多条件查询
{u’high’: 179, u’comment’: u’use update_many’, u’age’: 22, u’_id’: 5, u’name’: u’zhangjian’}
—————-IN
{u’high’: 173, u’comment’: u’use update_many’, u’age’: 26, u’_id’: 2, u’name’: u’zhang’}
—————-count
—————-条件或
大于等于29或者小于23
{u’high’: 180, u’comment’: u’use update_many’, u’age’: 29, u’_id’: 3, u’name’: u’wei’}
{u’high’: 158, u’age’: 30, u’_id’: 4, u’name’: u’weijian’}
{u’high’: 179, u’comment’: u’use update_many’, u’age’: 22, u’_id’: 5, u’name’: u’zhangjian’}
{u’high’: 176, u’comment’: u’use update_many’, u’age’: -27, u’_id’: 0, u’name’: u’zzzzz’}
{u’age’: 19, u’_id’: 100, u’list’: [2, 3, 5], u’name’: u’zwj’}
{u’age’: 19, u’_id’: 101, u’list’: [1, 2, 3, 4, 5, 6, 7], u’name’: u’zwj’}
————exists用法
age exists {u’high’: 173, u’comment’: u’use update_many’, u’age’: 26, u’_id’: 2, u’name’: u’zhang’}
age exists {u’high’: 180, u’comment’: u’use update_many’, u’age’: 29, u’_id’: 3, u’name’: u’wei’}
age exists {u’high’: 158, u’age’: 30, u’_id’: 4, u’name’: u’weijian’}
age exists {u’high’: 179, u’comment’: u’use update_many’, u’age’: 22, u’_id’: 5, u’name’: u’zhangjian’}
age exists {u’high’: 176, u’comment’: u’use update_many’, u’age’: -27, u’_id’: 0, u’name’: u’zzzzz’}
age exists {u’age’: 19, u’_id’: 100, u’list’: [2, 3, 5], u’name’: u’zwj’}
age exists {u’age’: 19, u’_id’: 101, u’list’: [1, 2, 3, 4, 5, 6, 7], u’name’: u’zwj’}
age not exists {u’comment’: u’after replace_one operation just exists comment(key)’, u’_id’: 1}
正则表达式查询
{u’high’: 180, u’comment’: u’use update_many’, u’age’: 29, u’_id’: 3, u’name’: u’wei’}
{u’high’: 158, u’age’: 30, u’_id’: 4, u’name’: u’weijian’}
{u’high’: 173, u’comment’: u’use update_many’, u’age’: 26, u’_id’: 2, u’name’: u’zhang’}
{u’high’: 179, u’comment’: u’use update_many’, u’age’: 22, u’_id’: 5, u’name’: u’zhangjian’}
——————–使用sort排序(文档中没有排序的字段也会打印出来,表示最小)
————–age 升序
{u’comment’: u’after replace_one operation just exists comment(key)’, u’_id’: 1}
{u’high’: 176, u’comment’: u’use update_many’, u’age’: -27, u’_id’: 0, u’name’: u’zzzzz’}
{u’age’: 19, u’_id’: 100, u’list’: [2, 3, 5], u’name’: u’zwj’}
{u’age’: 19, u’_id’: 101, u’list’: [1, 2, 3, 4, 5, 6, 7], u’name’: u’zwj’}
{u’high’: 179, u’comment’: u’use update_many’, u’age’: 22, u’_id’: 5, u’name’: u’zhangjian’}
{u’high’: 173, u’comment’: u’use update_many’, u’age’: 26, u’_id’: 2, u’name’: u’zhang’}
{u’high’: 180, u’comment’: u’use update_many’, u’age’: 29, u’_id’: 3, u’name’: u’wei’}
{u’high’: 158, u’age’: 30, u’_id’: 4, u’name’: u’weijian’}
————–age 降序
{u’high’: 158, u’age’: 30, u’_id’: 4, u’name’: u’weijian’}
{u’high’: 180, u’comment’: u’use update_many’, u’age’: 29, u’_id’: 3, u’name’: u’wei’}
{u’high’: 173, u’comment’: u’use update_many’, u’age’: 26, u’_id’: 2, u’name’: u’zhang’}
{u’high’: 179, u’comment’: u’use update_many’, u’age’: 22, u’_id’: 5, u’name’: u’zhangjian’}
{u’age’: 19, u’_id’: 100, u’list’: [2, 3, 5], u’name’: u’zwj’}
{u’age’: 19, u’_id’: 101, u’list’: [1, 2, 3, 4, 5, 6, 7], u’name’: u’zwj’}
{u’high’: 176, u’comment’: u’use update_many’, u’age’: -27, u’_id’: 0, u’name’: u’zzzzz’}
{u’comment’: u’after replace_one operation just exists comment(key)’, u’_id’: 1}
————–age升序,high升序
{u’comment’: u’after replace_one operation just exists comment(key)’, u’_id’: 1}
{u’high’: 176, u’comment’: u’use update_many’, u’age’: -27, u’_id’: 0, u’name’: u’zzzzz’}
{u’age’: 19, u’_id’: 100, u’list’: [2, 3, 5], u’name’: u’zwj’}
{u’age’: 19, u’_id’: 101, u’list’: [1, 2, 3, 4, 5, 6, 7], u’name’: u’zwj’}
{u’high’: 179, u’comment’: u’use update_many’, u’age’: 22, u’_id’: 5, u’name’: u’zhangjian’}
{u’high’: 173, u’comment’: u’use update_many’, u’age’: 26, u’_id’: 2, u’name’: u’zhang’}
{u’high’: 180, u’comment’: u’use update_many’, u’age’: 29, u’_id’: 3, u’name’: u’wei’}
{u’high’: 158, u’age’: 30, u’_id’: 4, u’name’: u’weijian’}
————–age升序,high降序
{u’comment’: u’after replace_one operation just exists comment(key)’, u’_id’: 1}
{u’high’: 176, u’comment’: u’use update_many’, u’age’: -27, u’_id’: 0, u’name’: u’zzzzz’}
{u’age’: 19, u’_id’: 100, u’list’: [2, 3, 5], u’name’: u’zwj’}
{u’age’: 19, u’_id’: 101, u’list’: [1, 2, 3, 4, 5, 6, 7], u’name’: u’zwj’}
{u’high’: 179, u’comment’: u’use update_many’, u’age’: 22, u’_id’: 5, u’name’: u’zhangjian’}
{u’high’: 173, u’comment’: u’use update_many’, u’age’: 26, u’_id’: 2, u’name’: u’zhang’}
{u’high’: 180, u’comment’: u’use update_many’, u’age’: 29, u’_id’: 3, u’name’: u’wei’}
{u’high’: 158, u’age’: 30, u’_id’: 4, u’name’: u’weijian’}
————-$all
{u’age’: 19, u’_id’: 101, u’list’: [1, 2, 3, 4, 5, 6, 7], u’name’: u’zwj’}
————-$in
{u’age’: 19, u’_id’: 100, u’list’: [2, 3, 5], u’name’: u’zwj’}
{u’age’: 19, u’_id’: 101, u’list’: [1, 2, 3, 4, 5, 6, 7], u’name’: u’zwj’}
————-$size用法
————-size=3
{u’age’: 19, u’_id’: 100, u’list’: [2, 3, 5], u’name’: u’zwj’}
————-size=7
{u’age’: 19, u’_id’: 101, u’list’: [1, 2, 3, 4, 5, 6, 7], u’name’: u’zwj’}
——————-$unset用法
—before
{u’high’: 158, u’age’: 30, u’_id’: 4, u’name’: u’weijian’}
—after
{u’high’: 158, u’_id’: 4, u’name’: u’weijian’}

 

result = db.devices.find({‘$and’:[{‘stats.fwver’: v},{‘username’:{‘$regex’:r'(?i)ltu’}}]})#匹配ltu

result = db.devices.find({‘$and’:[{‘stats.fwver’: v},{‘username’:{‘$regex’:r’^[\d+]’}}]})#匹配数字

 

这里topic_question中的topic_list是一个数组,需要查找topic_list中匹配给定两个元素的条目

listf=list()
results = db.topic_questions.find({‘$and’: [{‘topic_list’:source}, {‘topic_list’: target}]})
for result in results:
listf.append({‘question’: result[‘title’], ‘answers_count’: result[‘answers_count’]})

———————
作者:yfraquelle
来源:CSDN
原文:https://blog.csdn.net/y_f_raquelle/article/details/83897793
版权声明:本文为博主原创文章,转载请附上博文链接!

 


 

数据库

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个文档

转载请注明:SuperIT » python mongodb 操作

喜欢 (0)or分享 (0)