mongodb安装及基础命令


安装mongodb(mongodb-linux-x86_64-3.2.4.tgz)
1 export PATH=$PATH:/usr/local/mongodb/bin
2 /usr/local/mongodb/bin 新增mongodb.conf
port=27017
dbpath=/opt/mongodb/data/db
logpath=/opt/mongodb/logs/mongodb.log
logappend=true
fork=true
3 ./mongod -f mongodb.conf服务端启动
./mongod -f mongodb.conf --rest

4 停止
use admin
db.shutdownServer();

使用的是【Spring 3.1.2.RELEASE 版本】 + 【spring-data-mongodb 1.3.0.RELEASE】 + 【mongo-java-driver 2.11.1】

show dbs;
db.logfile.stats();
db.collection.stats();
db.logfiledb.stats();
db.collection.find()
db.collection.findOne()
db.getCollection("foo-ee") 得到foo-ee集合

db.collection.remove() //集合本身和索引都会保留
db.drop_collection("collection") //整个集合和索引都被删除
db.tbname.drop()

db.update{criterial,{"$inc":{"pageviews":1}}} //值加1
db.update{criterial,{"$set":{"key":"val"}}} //值修改
db.update{criterial,{"$push":{"key":"val"}}} //数组push

db.update{{"key":{"$ne":"temp"}},{"$push":{"key":"temp"}}} //如果数组中不存在temp,则插入
db.update{criterial,{"$addToSet":{"key":"temp"}}} //可以插入时避免重复
db.update{criterial,{"$addToSet":{"key":{"$each":["val1","val1","val1"]}}}} //addToSet和each组合
db.update({},{"$pull":{"key":"val1"}}) //将所有匹配的部分删掉
db.update({"comments.author":"jhone"},{"$set":{"comments.$.author":"jim"}})//用定位符替换数组中符合条件的名字
db.update({"count":25},{"$inc":{"count":3}},true) //第三个参数true代表是upsert 没有匹配的,增加一个25的,再增加为28

db.find(criterial,{"key1":1,"key2":0}) //返回值包含key1,不含key2

start=new Date("2015-01-01")
db.find({"stat_date",{"$lt":start}} //lt gt lte gte Date精确到毫秒
db.find({"key":{"$ne":"val"}} //key!=val

db.find({"$or":[{"key1":{"$in":[123,"456"]}},{"key2":"val2"}]}) //$in多个不同类型的值 $nin $or
db.find({"key":{"$not":{"$mod":[5,1]}}) //$mod取余 %5=1的值 $not

db.find({"key":{"$in":[null],"$exists":true}}) //键存在并且值为null
db.find({"key":null}) //值为null或者不存在该键的文档
db.find({"key":/val/i}) //正则

数组
db.food.insert({"fruit":["apple","banana","peach"]})
db.food.find({"fruit":"apple"})
db.food.find({"fruit":{"$all":["apple","banana"]}}) //同时包含
db.food.find({"fruit.2":"banana"})

db.food.find({"fruit":{"$size":3}}) //只能用于查找定长数组,无法使用范围查询
db.food.update({"$push":{"fruit":"strawberry"},"$inc":{"size":1}})
db.food.find({"size":{"$gt":3}}) //增加size,实现范围查询

db.log.findOne(criterial,{"comments":{"$slice":-10}}) //返回数组后十个及文档所有字段
db.log.findOne(criterial,{"comments":{"$slice":10}}) //返回数组前十个及文档所有字段
db.log.findOne(criterial,{"comments":{"$slice":[25,10]}}) //返回数组第25个元素到35及文档所有字段

db.log.find({"name":{"first":"je","last":"fa"}}) //固定顺序查找,完全精确匹配
db.log.find({"name.first":"je,"name.last":"fa"}) //无固定顺序
db.log.find({"comments":{"name.first":"je,"name.last":"fa"}}) //comments保存数组,只要comments里面的多个内嵌文档加起来包含两个值即可,与下面一个区别
db.log.find({"comments":{"$elemMatch":{"name.first":"je,"name.last":"fa"}}}) //comments保存数组,elemMatch指定了数组单个内嵌文档限定条件

//where不能使用索引
db.log.find({"$where":"this.x+this.y==10"})
db.log.find({"$where":"function(){return this.x+this.y==10;}"})

db.stock.find({"desc":"mp3"}).limit(50).skip(50).sort({uname:1,age:-1}) //skip太多影响性能,sort 1升序 -1降序 分页查找

//索引
db.log.ensureIndex({"date":1,"uname":1},{"name":"date_1_uname_1"}) //索引名称
db.log.ensureIndex({"date":1},{"unique":true},{"background":true}) //索引名称 后台处理可以防止建立索引时阻塞请求
db.stock.find({"desc":"mp3"}).hint({"desc":1})

//地理空间索引时假设在一个平面上
//按照精纬度查找靠近的点文档
{"gps":{"x":-30,"y":30}}
db.map.ensureIndex({"gps":"2d"},{"min":-1000,"max":1000})
db.map.find({"gps":{"$near":[40,-73]}}).limit(10)
db.runCommand({geoNear:"map",near:[40,-73],num:10})
//按照精纬度查找咖啡店
db.map.ensureIndex({"gps":"2d","desc":1})
db.map.find({"gps":{"$near":[40,-73]},"desc":"coffee"}).limit(1)

db.log.count()
db.log.count({"x":1})

[{"day":"2010-10-03","time":"2010-10-03 03:55:22",price:3},{"day":"2010-10-03","time":"2010-10-03 04:55:22",price:3},{"day":"2010-10-03","time":"2010-10-03 05:55:22",price:3}]
db.runCommand({"distinct","tbname","key":"userid"})
db.runCommand({"group":{
"ns":"gp", //集合名
"key":{"day":true}, //分组字段
"initial":{"time":0},
"$reduce":function(doc,prev){ //max
if(doc.time>prev.time){
prev.price=doc.price;
prev.time=doc.time;
}
},
"$finalize":function(prev){ //改变返回结果
if(prev.price=3){
delete prev.price
}
}
}
})
mongodb 规定 group一次的记录不能超过10000条,超过10000条的 必须用map reduce计算

//map reduce
map=function(){
for(var key in this){
emit(key,{count:1});
}
}
reduce=function(key,emits){
total=0;
for(var i in emits){
total+=emits[i].count;
}
return {"count":total};
}
mr=db.runCommand({"mapreduce":"tbname","map":map,"reduce":reduce})
db[mr.result].find()

db.listCommands()

//固定集合,可以在程序中使用尾部游标获取新添加的文档
db.createCollection("collection",{capped:true,size:10000,max:100}) //创建固定集合,大小事10000字节
db.createCollection("mycappedcoll",{capped:true,size:10000,max:100,autoIndexId:false})
db.my_collection.find().sort({"$natural":-1}) //按照插入顺序查询也就是自然顺序,-1是反向

//创建用户
db.createRole(
{
role: "root",
privileges: [
{ resource: { db: "logfile", collection: "" }, actions: [ "find", "update", "insert", "remove" ] }
],
roles: []
}
)
db.createUser(
{
user: "root",
pwd: "pwd",
roles: [root] //root默认超级管理员
}
)
db.createUser( {
user: "logfile",
pwd: "pwd",
roles: [ { role: "root", db: "logfile" } ]
});

db.getUsers()
db.grantRolesToUser("root",[{role:"root", db:"logfile"}])
db.auth("root","jscnbi_123654")

http://www.oracle.com/technetwork/java/javase/downloads/index.html
原文地址:https://www.cnblogs.com/ai464068163/p/5422150.html