mongoDB数据库基本操作

来源:互联网 发布:h5 刮刮卡 源码 编辑:程序博客网 时间:2024/06/05 03:29

查看当前数据库

MongoDB shell version:1.8.1
connecting to:test
> db
test

 

查看全部数据库列表

>show dbs
ChatRoom    0.03125GB
admin       (empty)
local       (empty)

 

切换数据库

>use ChatRoom
switched to db ChatRoom
>db
ChatRoom

 

删除数据库

复制代码
>db
ChatRoom
>show dbs
ChatRoom    0.03125GB
admin       (empty)
local       (empty)
>db.dropDatabase()
{
"dropped":"ChatRoom","ok":1}
>show dbs
admin       (empty)
local       (empty)
复制代码

 

 MongoDB帮助主题help

复制代码
> help
        db.help()                    help on db methods
        db.mycoll.help()             help on collection methods
        rs.help()                    help on replica set methods
        help connect                 connecting to a db help
        help admin                   administrative help
        help misc                    misc things to know
        help mr                      mapreduce help

        show dbs                     show database names
        show collections             show collections 
in current database
        show users                   show users 
in current database
        show profile                 show most recent system.profile entries with time
>= 1ms
        use 
<db_name>                set current database
        db.foo.find()                list objects 
in collection foo
        db.foo.find( { a : 
1 } )     list objects in foo where a == 1
        it                           result of the last line evaluated; use to furtheriterate
        DBQuery.shellBatchSize 
= x   set default number of items to display on shell
        exit                         quit the mongo shell
复制代码

 

复制代码
> db.help()
DB methods:
        db.addUser(username, password[, readOnly
=false])
        db.auth(username, password)
        db.cloneDatabase(fromhost)
        db.commandHelp(name) returns the help 
for the command
        db.copyDatabase(fromdb, todb, fromhost)
        db.createCollection(name, { size : ..., capped : ..., max : ... } )
        db.currentOp() displays the current operation 
in the db
        db.dropDatabase()
        db.eval(func, args) run code server
-side
        db.getCollection(cname) same as db[
'cname'or db.cname
        db.getCollectionNames()
        db.getLastError() 
- just returns the err msg string
        db.getLastErrorObj() 
- return full status object
        db.getMongo() get the server connection object
        db.getMongo().setSlaveOk() allow this connection to read 
from the nonmaster member of a replica pair
        db.getName()
        db.getPrevError()
        db.getProfilingLevel() 
- deprecated
        db.getProfilingStatus() 
- returns if profiling is on and slow threshold
        db.getReplicationInfo()
        db.getSiblingDB(name) get the db at the same server as this one
        db.isMaster() check replica primary status
        db.killOp(opid) kills the current operation 
in the db
        db.listCommands() lists all the db commands
        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.serverStatus()
        db.setProfilingLevel(level,
<slowms>) 0=off 1=slow 2=all
        db.shutdownServer()
        db.stats()
        db.version() current version of the server
        db.getMongo().setSlaveOk() allow queries on a replication slave server

从指定主机上克隆数据库
> db.cloneDatabase(“127.0.0.1”); 

从指定的机器上复制指定数据库数据到某个数据库
> db.copyDatabase("mydb", "temp", "127.0.0.1");

修复当前数据库
> db.repairDatabase();

查看当前使用的数据库(db和getName方法是一样的效果,都可以查询当前使用的数据库
> db.getName();  
> db;

显示当前db状态
> db.stats();

当前db版本
> db.version();

查看当前db的链接机器地址
> db.getMongo(); 

Ø Collection聚集集合

1、创建一个聚集集合(table)

[html] view plain copy
  1. > db.createCollection(“collName”, {size: 20, capped: 5, max: 100});  

2、得到指定名称的聚集集合(table)

[html] view plain copy
  1. > db.getCollection("account");  

3、得到当前db的所有聚集集合

[html] view plain copy
  1. > db.getCollectionNames();  

4、显示当前db所有聚集索引的状态

[html] view plain copy
  1. > db.printCollectionStats();  

Ø 用户相关

1、添加一个用户

[html] view plain copy
  1. > db.addUser("name");  
  2. > db.addUser("userName", "pwd123", true);  

添加用户、设置密码、是否只读

2、数据库认证、安全模式

[html] view plain copy
  1. > db.auth("userName", "123123");  

3、显示当前所有用户

[html] view plain copy
  1. > show users;  

4、删除用户

[html] view plain copy
  1. > db.removeUser("userName");  

Ø 其他

1、查询之前的错误信息

[html] view plain copy
  1. > db.getPrevError();  

2、清除错误记录

[html] view plain copy
  1. > db.resetError();   

三、Collection聚集集合操作

Ø 查看聚集集合基本信息

1、查看帮助

[html] view plain copy
  1. > db.yourColl.help();  

2、查询当前集合的数据条数

[html] view plain copy
  1. > db.yourColl.count();  

3、查看数据空间大小

[html] view plain copy
  1. > db.userInfo.dataSize();  

4、得到当前聚集集合所在的db

[html] view plain copy
  1. > db.userInfo.getDB();  

5、得到当前聚集的状态

> db.userInfo.stats();

6、得到聚集集合总大小

> db.userInfo.totalSize();

7、聚集集合储存空间大小

> db.userInfo.storageSize();

8、Shard版本信息

> db.userInfo.getShardVersion()

9、聚集集合重命名

> db.userInfo.renameCollection("users");

将userInfo重命名为users

10、删除当前聚集集合

> db.userInfo.drop();



0 0
原创粉丝点击