MongoDb常用命令
2015-06-19 00:00
573 查看
mongodb端口为27017
It looks like you are trying to access MongoDB over HTTP on the native driver port.
mongodb的结构为database, collection,
创建database,
查看已经存在的db 及其占用空间
接下来在collection里添加json格式的内容
查看名为posts的collection的所有内容
查看已经有的collections
清空collection
删除collection
删除当前数据库
五,更多命令
db.AddUser(username,password) 添加用户
db.auth(usrename,password) 设置数据库连接验证
db.cloneDataBase(fromhost) 从目标服务器克隆一个数据库
db.commandHelp(name) returns the help for the command
db.copyDatabase(fromdb,todb,fromhost) 复制数据库fromdb—源数据库名称,todb—目标数据库名称,fromhost—源数据库服务器地址
db.createCollection(name,{size:3333,capped:333,max:88888}) 创建一个数据集,相当于一个表
db.currentOp() 取消当前库的当前操作
db.dropDataBase() 删除当前数据库
db.eval(func,args) run code server-side
db.getCollection(cname) 取得一个数据集合,同用法:db['cname'] or
db.getCollenctionNames() 取得所有数据集合的名称列表
db.getLastError() 返回最后一个错误的提示消息
db.getLastErrorObj() 返回最后一个错误的对象
db.getMongo() 取得当前服务器的连接对象get the server
db.getMondo().setSlaveOk() allow this connection to read from then nonmaster membr of a replica pair
db.getName() 返回当操作数据库的名称
db.getPrevError() 返回上一个错误对象
db.getProfilingLevel()
db.getReplicationInfo() 获得重复的数据
db.getSisterDB(name) get the db at the same server as this onew
db.killOp() 停止(杀死)在当前库的当前操作
db.printCollectionStats() 返回当前库的数据集状态
db.printReplicationInfo()
db.printSlaveReplicationInfo()
db.printShardingStatus() 返回当前数据库是否为共享数据库
db.removeUser(username) 删除用户
db.repairDatabase() 修复当前数据库
db.resetError()
db.runCommand(cmdObj) run a database command. if cmdObj is a string, turns it into {cmdObj:1}
db.setProfilingLevel(level) 0=off,1=slow,2=all
db.shutdownServer() 关闭当前服务程序
db.version() 返回当前MongoDB版本信息
db.test.find({id:10}) 返回test数据集ID=10的数据集
db.test.find({id:10}).count() 返回test数据集ID=10的数据总数
db.test.find({id:10}).limit(2) 返回test数据集ID=10的数据集从第二条开始的数据集
db.test.find({id:10}).skip(8) 返回test数据集ID=10的数据集从0到第八条的数据集
db.test.find({id:10}).limit(2).skip(8) 返回test数据集ID=1=的数据集从第二条到第八条的数据
db.test.find({id:10}).sort() 返回test数据集ID=10的排序数据集
db.test.findOne([query]) 返回符合条件的一条数据
db.test.getDB() 返回此数据集所属的数据库名称
db.test.getIndexes() 返回些数据集的索引信息
db.test.group({key:…,initial:…,reduce:…[,cond:...]})
db.test.mapReduce(mayFunction,reduceFunction,)
db.test.remove(query) 在数据集中删除一条数据
db.test.renameCollection(newName) 重命名些数据集名称
db.test.save(obj) 往数据集中插入一条数据
db.test.stats() 返回此数据集的状态
db.test.storageSize() 返回此数据集的存储大小
db.test.totalIndexSize() 返回此数据集的索引文件大小
db.test.totalSize() 返回些数据集的总大小
db.test.update(query,object[,upsert_bool]) 在此数据集中更新一条数据
db.test.validate() 验证此数据集
db.test.getShardVersion() 返回数据集共享版本号
六,MongoDB语法与现有关系型数据库SQL语法比较
MongoDB语法 MySql语法
db.test.find({‘name':’foobar’}) <==> select * from test where name=’foobar’
db.test.find() <==> select * from test
db.test.find({‘ID':10}).count() <==> select count(*) from test where ID=10
db.test.find().skip(10).limit(20) <==> select * from test limit 10,20
db.test.find({‘ID':{$in:[25,35,45]}}) <==> select * from test where ID in (25,35,45)
db.test.find().sort({‘ID':-1}) <==> select * from test order by ID desc
db.test.distinct(‘name’,{‘ID':{$lt:20}}) <==> select distinct(name) from test where ID<20
db.test.group({key:{‘name':true},cond:{‘name':’foo’},reduce:function(obj,prev){prev.msum+=obj.marks;},initial:{msum:0}}) <==> select name,sum(marks) from test group by name
db.test.find(‘this.ID<20′,{name:1}) <==> select name from test where ID<20
db.test.insert({‘name':’foobar’,’age':25})<==>insert into test (‘name’,’age’) values(‘foobar’,25)
db.test.remove({}) <==> delete * from test
db.test.remove({‘age':20}) <==> delete test where age=20
db.test.remove({‘age':{$lt:20}}) <==> elete test where age<20
db.test.remove({‘age':{$lte:20}}) <==> delete test where age<=20
db.test.remove({‘age':{$gt:20}}) <==> delete test where age>20
db.test.remove({‘age':{$gte:20}}) <==> delete test where age>=20
db.test.remove({‘age':{$ne:20}}) <==> delete test where age!=20
db.test.update({‘name':’foobar’},{$set:{‘age':36}}) <==> update test set age=36 where name=’foobar’
db.test.update({‘name':’foobar’},{$inc:{‘age':3}}) <==> update test set age=age+3 where name=’foobar’
注意以上命令大小写敏感
mongodb端口为27017
http://127.0.0.1:27017
It looks like you are trying to access MongoDB over HTTP on the native driver port.
mongodb的结构为database, collection,
创建database,
> use blog switched to db blog
查看已经存在的db 及其占用空间
> show dbs admin (empty) blog 0.078GB example 0.078GB local 0.078GB
接下来在collection里添加json格式的内容
> db.posts.insert({'title':'learn mongo','tag':'db'}) WriteResult({ "nInserted" : 1 }) > db.posts.insert({'title':'learn mysql','tag':'db'}) WriteResult({ "nInserted" : 1 })
查看名为posts的collection的所有内容
> db.posts.find() { "_id" : ObjectId("53d1b11aecbaca2dd97bc8d8"), "title" : "learn mongo", "tag" : "db" } { "_id" : ObjectId("53d1b124ecbaca2dd97bc8d9"), "title" : "learn mysql", "tag" : "db" }
查看已经有的collections
> show collections posts system.indexes
清空collection
> db.posts.remove() 2014-07-25T09:30:33.139+0800 remove needs a query at src/mongo/shell/collection.js:299
删除collection
> db.posts.drop() true
删除当前数据库
> db.dropDatabase()
五,更多命令
db.AddUser(username,password) 添加用户
db.auth(usrename,password) 设置数据库连接验证
db.cloneDataBase(fromhost) 从目标服务器克隆一个数据库
db.commandHelp(name) returns the help for the command
db.copyDatabase(fromdb,todb,fromhost) 复制数据库fromdb—源数据库名称,todb—目标数据库名称,fromhost—源数据库服务器地址
db.createCollection(name,{size:3333,capped:333,max:88888}) 创建一个数据集,相当于一个表
db.currentOp() 取消当前库的当前操作
db.dropDataBase() 删除当前数据库
db.eval(func,args) run code server-side
db.getCollection(cname) 取得一个数据集合,同用法:db['cname'] or
db.getCollenctionNames() 取得所有数据集合的名称列表
db.getLastError() 返回最后一个错误的提示消息
db.getLastErrorObj() 返回最后一个错误的对象
db.getMongo() 取得当前服务器的连接对象get the server
db.getMondo().setSlaveOk() allow this connection to read from then nonmaster membr of a replica pair
db.getName() 返回当操作数据库的名称
db.getPrevError() 返回上一个错误对象
db.getProfilingLevel()
db.getReplicationInfo() 获得重复的数据
db.getSisterDB(name) get the db at the same server as this onew
db.killOp() 停止(杀死)在当前库的当前操作
db.printCollectionStats() 返回当前库的数据集状态
db.printReplicationInfo()
db.printSlaveReplicationInfo()
db.printShardingStatus() 返回当前数据库是否为共享数据库
db.removeUser(username) 删除用户
db.repairDatabase() 修复当前数据库
db.resetError()
db.runCommand(cmdObj) run a database command. if cmdObj is a string, turns it into {cmdObj:1}
db.setProfilingLevel(level) 0=off,1=slow,2=all
db.shutdownServer() 关闭当前服务程序
db.version() 返回当前MongoDB版本信息
> db.version() 2.6.0
db.test.find({id:10}) 返回test数据集ID=10的数据集
db.test.find({id:10}).count() 返回test数据集ID=10的数据总数
db.test.find({id:10}).limit(2) 返回test数据集ID=10的数据集从第二条开始的数据集
db.test.find({id:10}).skip(8) 返回test数据集ID=10的数据集从0到第八条的数据集
db.test.find({id:10}).limit(2).skip(8) 返回test数据集ID=1=的数据集从第二条到第八条的数据
db.test.find({id:10}).sort() 返回test数据集ID=10的排序数据集
db.test.findOne([query]) 返回符合条件的一条数据
db.test.getDB() 返回此数据集所属的数据库名称
db.test.getIndexes() 返回些数据集的索引信息
db.test.group({key:…,initial:…,reduce:…[,cond:...]})
db.test.mapReduce(mayFunction,reduceFunction,)
db.test.remove(query) 在数据集中删除一条数据
db.test.renameCollection(newName) 重命名些数据集名称
db.test.save(obj) 往数据集中插入一条数据
db.test.stats() 返回此数据集的状态
db.test.storageSize() 返回此数据集的存储大小
db.test.totalIndexSize() 返回此数据集的索引文件大小
db.test.totalSize() 返回些数据集的总大小
db.test.update(query,object[,upsert_bool]) 在此数据集中更新一条数据
db.test.validate() 验证此数据集
db.test.getShardVersion() 返回数据集共享版本号
六,MongoDB语法与现有关系型数据库SQL语法比较
MongoDB语法 MySql语法
db.test.find({‘name':’foobar’}) <==> select * from test where name=’foobar’
db.test.find() <==> select * from test
db.test.find({‘ID':10}).count() <==> select count(*) from test where ID=10
db.test.find().skip(10).limit(20) <==> select * from test limit 10,20
db.test.find({‘ID':{$in:[25,35,45]}}) <==> select * from test where ID in (25,35,45)
db.test.find().sort({‘ID':-1}) <==> select * from test order by ID desc
db.test.distinct(‘name’,{‘ID':{$lt:20}}) <==> select distinct(name) from test where ID<20
db.test.group({key:{‘name':true},cond:{‘name':’foo’},reduce:function(obj,prev){prev.msum+=obj.marks;},initial:{msum:0}}) <==> select name,sum(marks) from test group by name
db.test.find(‘this.ID<20′,{name:1}) <==> select name from test where ID<20
db.test.insert({‘name':’foobar’,’age':25})<==>insert into test (‘name’,’age’) values(‘foobar’,25)
db.test.remove({}) <==> delete * from test
db.test.remove({‘age':20}) <==> delete test where age=20
db.test.remove({‘age':{$lt:20}}) <==> elete test where age<20
db.test.remove({‘age':{$lte:20}}) <==> delete test where age<=20
db.test.remove({‘age':{$gt:20}}) <==> delete test where age>20
db.test.remove({‘age':{$gte:20}}) <==> delete test where age>=20
db.test.remove({‘age':{$ne:20}}) <==> delete test where age!=20
db.test.update({‘name':’foobar’},{$set:{‘age':36}}) <==> update test set age=36 where name=’foobar’
db.test.update({‘name':’foobar’},{$inc:{‘age':3}}) <==> update test set age=age+3 where name=’foobar’
注意以上命令大小写敏感
相关文章推荐
- nagios 监控 mongodb 解决nagios界面显示null 问题
- MongoDB的存储结构及对空间使用率的影响
- MongoDB远程主从部署下的全量数据同步及故障恢复策略
- Mongodb从库数据重新初始化步骤
- Mongodb安装,主从部署
- MongoDB安装及复制集搭建
- Mongodb安装、简单使用及与spring的集成
- mongodb sharding
- 菜鸟的mongoDB学习---(二)MongoDB 数据库,对象,集合
- MongoDB副本集成员状态
- MongoDB副本集成员状态
- 菜鸟的mongoDB学习---(一)安装与简介
- 在WIN7下安装运行mongodb
- Mongodb的副本、分片搭建方式
- MongoDB副本集配置系列七:MongoDB oplog详解
- mongodb update更新一条和更新多条
- mongodb 学习笔记 01 -- 概述,安装,运行
- mongodb安装及启动
- centos linux lampp安装mongodb 驱动
- Mongodb的一些小技巧