一、预备工作
http://blog.csdn.net/yuwenruli/article/details/8529192
2.windows下mongodb安装和启动配置
http://www.cnblogs.com/linjiqin/p/3192159.html
3.linux mongo启动 /关闭
1.启动
./mongod –storageEngine mmapv1 –dbpath=/usr/gongbg/mongo_data/data–logpath=/usr/gongbg/mongo_data/logs–logappend –port=27017–fork –auth
2.关闭
./mongouseadmindb.shutdownServer()
3. 创建用户
具体步骤如下
a.首先用admin用户登陆
b.选择想创建用户的数据库
c.创建用户
use admindb.createUser( {“user”:”xxx”,”pwd”:”xxx”,”roles”: [ {role:”clusterAdmin”,db:”admin”}, {role:”readAnyDatabase”,db:”admin”},”readWrite”] } )use xxx(dbname)db.createUser({user:”baidubaike_”,pwd:”baidubaike_123″,roles:[“readWrite”]})
各种权限解释
Built-In Roles(内置角色): 1. 数据库用户角色:read、readWrite; 2. 数据库管理角色:dbAdmin、dbOwner、userAdmin; 3. 集群管理角色:clusterAdmin、clusterManager、clusterMonitor、hostManager; 4. 备份恢复角色:backup、restore; 5. 所有数据库角色:readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase 6. 超级用户角色:root // 这里还有几个角色间接或直接提供了系统超级用户的访问(dbOwner 、userAdmin、userAdminAnyDatabase) 7. 内部角色:__system
4.Failed global initialization: BadValue Invalid or no user locale set. Please ensure LANG and/or LC_* environment variables are set correctly
命令行输入 export LC_ALL=C 即可
5.Mongo VUE 可以和 MongoDB 连接,可是无法打开 collection
http://www.oschina.net/question/1995999_2148135
6.mongo文章
http://www.360doc.com/content/15/1010/15/1519870_504671696.shtml
二、mongo 查询
0.官方文档
https://docs.mongodb.com/manual/tutorial/project-fields-from-query-results/
1. 包含省略号查询
{ title:/\\.{3}$/}
2.是否包含
{“hasfastPhoto”: {“$exists”:true}}
3.小于
{“imgTxtUpdateFlag”: { $lt:7}}
4.或者
{“$or”: [ {“hasfastPhoto”:1}, {“imgTxtUpdateFlag”:7} ]}
5.等于
{url:’http://zhidao.baidu.com/question/1539766649154449027.html’}
6.#在mongovue下使用
{sort} {“insertTime” : -1}
7.and查询
{“$and”: [ {“insertTime”: {“$gte”:”2015-02-06 14:51:04″,”$lte”:”2015-02-06 14:52:03″} } ]}
8.like查询
① 普通like查询
{“u”: /.content.*/}
② 忽略大小写
{title:{‘$regex’:’Vitamin’,’$options’:’i’}}
9.如果检索需要不区分大小写,我们可以设置 $options 为 $i。
{“u”: {$regex:’news.163.com’,$options:’i’}}
10.not查询
{“u”: {$not:/.content.*/}}
11.子文档like查询
{“markingTagList”: {“$elemMatch”: {“appName”: ‘政策’,”currentTagsArray”: ‘国办发’ } }}
{“similarList.title”:/.*302*./}
12.or查询
{“$or”: [ {“poemCon”: /.①.*/ },{“poemCon”: /.②.*/ } ]}
13.and,or组合
{“$and”: [ {“$or”: [ {“reported”: {“$exists”:false} }, {“reported”:0} ] }, {“$or”: [ {“hidden”: {“$exists”:false} }, {“hidden”:1} ] } ],”_id”:1081}
14.mongo分组查询(mongovue)
Group中 {“city”:true}Reduce中 Initial Value: {“count”:0}function Reduce(doc, out){ out.count +=1}Finalize中function Finalize(out){returnout; }
原生写法db.getCollection(‘drugfuture’).group({ key: { pageNo:true}, cond: { startTime:’2015-04-01′,endTime:’2015-05-01′}, $reduce:function ( curr, result ) { result.total +=1; }, initial: { total :0} } )
对group后的数据进行过滤db.yiMaiTongFinal2.aggregate( [ {$group:{_id :”$批准文号:”,count :{$sum :1}}}, {$match:{count:{$gt:1}}} ] )
多字段聚合(limit范围0~50)db.bysy_chufang_repeat_drug_disease.aggregate( [ {$group:{ _id:{drugName:”$drugName”,disease:”$disease”} ,count:{$sum:1} } },{$sort:{count:-1} } ,{$limit :50} ])
15.查询系统中所有用户
db.system.users.find();
三、mongo 修改:
1.设置st字段为0
{ $set: { st:0}}
四、mongo删除
1.mongo删除字段
{ $unset:{“story”:””}}
五、mongo插入(ps:mongovue)
1.在创建的表中新增列与数据,右击表选择Insert document
六、mongodump备份
mongodump –archive –db jinjing –host 127.0.0.1 –port 10088 | mongorestore –archive –host 182.92.202.151 –port 27017
其他、一些实践性的东西
mongo学习网站
http://www.runoob.com/mongodb/mongodb-mongodump-mongorerstore.html
mongodb 分片实战
http://www.cnblogs.com/magialmoon/archive/2013/04/10/3013121.html
pymongo使用示例
1.一次更新多条
db.collection.update({}, {‘$set’ : {‘salary’ : 10000}}, upsert=False, multi=True)
mongo导出
a>linux
./mongoexport –db baiduBaike –collection url –out /home/gongbg/url.json
b>windows
D:\java\MongoDB 2.6 Standard\bin>mongoexport.exe -d chat -c chat -q {“qudao”:3},{“ques”:1,”ans”:1,”_id”:0} –csv -f ques,ans -o temp.csv
官方文档
http://docs.mongoing.com/manual-zh/reference/program/mongoexport.html
The –journal option is the one causing the problem as that option is removed from Mongo 3.0.0
Remove the –journal option and it should work fine
mongo常规备份
http://www.cnblogs.com/yaoxing/p/mongodb-backup-rules.html (转)
mongo有大量重复数据,在3.0以后不支持建索引。
dropDups选项为true时会删除集合的重复值,但现在报错了,经过查询dropDups不支持mongodb3版本了
用以下方法解决在重复数据的集合里建立唯一索引
1.首先把集合数据导出
./mongoexport –db baiduBaike –collection url –out /home/gongbg/url.json
2.删除集合数据
db.CollectedUrl.remove({})
3.在集合上创建唯一索引
db.CollectedUrl.ensureIndex({uri:1},{unique:true})
4.把导出数据再次导入集合中
./mongoimport –db baiduBaike –collection url –upsert /home/gongbg/url.json
注意要加上–upsert选项
–upsert insert or update objects that already exist
mongodb windows分片配置
http://www.cnblogs.com/huangxincheng/archive/2012/03/07/2383284.html
mongo linux 分片配置
mongod –fork –dbpath/home/gongbg/mongo_data/data/config –logpath /home/gongbg/mongo_data/log/config/config.log –port10000–storageEngine mmapv1mongos –port20000–configdb172.20.0.26:10000–logpath/home/gongbg/mongo_data/log/mongos/mongos.log –forkmongod –dbpath/home/gongbg/mongo_data/data/shard/–logpath/home/gongbg/mongo_data/log/shard/shard.log –fork –port27019–storageEngine mmapv1./mongod –storageEngine mmapv1 –dbpath=/home/gongbg/mongo_data/data/shard/ –logpath=/home/gongbg/mongo_data/log/shard/shard.log –logappend –port=27020–forkmongo –port20000use admindb.runCommand({addshard:”172.20.0.26:27019″})db.runCommand({addshard:”172.20.0.18:27020″})db.runCommand( {enablesharding :”baiduBaike”} )db.runCommand( {shardcollection :”baiduBaike.content”,key :{url :1} } ) db.printShardingStatus();
mongo错误(导致启动不了)
openExisting size 38273024 less than minimum file size expectation /home/gongbg/mongo_data/data/baiduBaike.13
把baiduBaike.13删掉就好 简单粗暴 不过不知道啥后果