mongoDB 最近开始做一些简单的数据查询计算功能,但是因为mongodb用的比较少,对很多的查询方法都不熟悉,因此整理了一些常用的命令,平时工作有需要时候方便查询,当然遇到一些新的也可以进行更新。
db.createUser( { user: “myUserAdmin”, pwd: “abc123”, roles: [ { role: “userAdminAnyDatabase”, db: “admin” } ] } )
mongo –port 27017 -u “myUserAdmin” -p “abc123” –authenticationDatabase “admin”
mongo –port 27017 -u “myTester” -p “xyz123” –authenticationDatabase “test”
use nieweb
db.dropUser(“dev”) true
db.createUser({“user”:”dev”,pwd:”dev123”,roles:[“readWrite”]}) Successfully added user: { “user” : “dev”, “roles” : [ “readWrite” ] } show users { “_id” : “nieweb.dev”, “user” : “dev”, “db” : “nieweb”, “roles” : [ { “role” : “readWrite”, “db” : “nieweb” } ] }
修改mongodb 外部访问的IP权限 /etc/mongod.conf 修改/etc/mongod.conf bingip改为0.0.0.0 或者外网IP
MySQL:
SELECT * FROM user
Mongo:
db.user.find()
MySQL:
SELECT * FROM user WHERE name = ‘foobar’
Mongo:
db.user.find({‘name’ : ‘foobar’})
MySQL:
INSERT INTO user (name, age) values (‘foobar’,25)
Mongo:
db.user.insert({‘name’ : ‘foobar’, ‘age’ : 25})
if you want add a column email on MySQL,you must :
ALTER TABLE user add email varchar(100) comment “邮箱”;
But in Mongo,you can just:
db.user.insert({‘name’ : ‘foobar’, ‘age’ : 25, ‘email’ : ‘foo@bar.com’})
MySQL:
DELETE * FROM user
Mongo:
db.user.remove({})
MySQL:
DELETE FROM user WHERE age < 30
Mongo:
db.user.remove({‘age’ : {$lt : 30}})
其他比较符号
gt:>; gte : >= ; lt:<; lte : <= ; $ne : !=
MySQL:
UPDATE user SET age = 36 WHERE name = ‘foobar’
Mongo:
db.user.update({‘name’ : ‘foobar’}, {$set : {‘age’ : 36}})
MySQL:
UPDATE user SET age = age + 3 WHERE name = ‘foobar’
Mongo:
db.user.update({‘name’ : ‘foobar’}, {$inc : {‘age’ : 3}})
查询满足下面集合中满足 gzh 与 channelId条件并对userNum求和。
{_id:"1",gzh:"abc",channelId:"1","date":"2017-8-01","userNum":1232} {_id:"1",gzh:"abc",channelId:"2","date":"2017-8-02","userNum":1232} {_id:"1",gzh:"abc",channelId:"3","date":"2017-8-03","userNum":1632} {_id:"1",gzh:"abc",channelId:"4","date":"2017-8-04","userNum":132} {_id:"1",gzh:"abc",channelId:"5","date":"2017-8-05","userNum":232} {_id:"1",gzh:"abc",channelId:"6","date":"2017-8-06","userNum":123} {_id:"1",gzh:"abc",channelId:"1","date":"2017-7-01","userNum":132} {_id:"1",gzh:"abc",channelId:"2","date":"2017-7-02","userNum":162} {_id:"1",gzh:"abc",channelId:"3","date":"2017-7-03","userNum":232} {_id:"1",gzh:"abc",channelId:"4","date":"2017-7-04","userNum":432} {_id:"1",gzh:"abc",channelId:"5","date":"2017-7-05","userNum":232} {_id:"1",gzh:"abc",channelId:"6","date":"2017-7-06","userNum":12} db.cllection.aggregate([ { $match : { gzh:"abc",channelId:"123"} }, { $group : { _id:"channelId", userSum:{$sum:'$userNum'} } } ]);根据gzh与channeId 添加下userNum的和。 其中channelId 可以是给定的任意多个。给出的collection的item如下:
{_id:"1",gzh:"abc",channelId:"1","date":"2017-8-01","userNum":1232} {_id:"1",gzh:"abc",channelId:"2","date":"2017-8-02","userNum":1232} {_id:"1",gzh:"abc",channelId:"3","date":"2017-8-03","userNum":1632} {_id:"1",gzh:"abc",channelId:"4","date":"2017-8-04","userNum":132} {_id:"1",gzh:"abc",channelId:"5","date":"2017-8-05","userNum":232} {_id:"1",gzh:"abc",channelId:"6","date":"2017-8-06","userNum":123} {_id:"1",gzh:"abc",channelId:"1","date":"2017-7-01","userNum":132} {_id:"1",gzh:"abc",channelId:"2","date":"2017-7-02","userNum":162} {_id:"1",gzh:"abc",channelId:"3","date":"2017-7-03","userNum":232} {_id:"1",gzh:"abc",channelId:"4","date":"2017-7-04","userNum":432} {_id:"1",gzh:"abc",channelId:"5","date":"2017-7-05","userNum":232} {_id:"1",gzh:"abc",channelId:"6","date":"2017-7-06","userNum":12}根据上面给出的集合,我们可以下如下的mapreduce来进行计算。
db.collection.mapReduce( function() { var channelIds = ["1", "2", "3", ]; if(channelIds.indexOf(this.channelId) >= 0) { emit( this.gzh,this.userNum); } }, function(key, values) { var sum = 0; for(var i = 0; i < values.length; i++) { sum = sum + values[i]; } return sum; }, { query:{gzh:"abc"}, out:"order_totals", finalize:function(key, reducedVal) { print(reducedVal) return reducedVal; } } ).find();最后运行输入:
{ “_id” : “abc”, “value” : xxxx }
mongodb指导手册