mongodb database operations create switch delete

  • 2020-05-07 20:37:59
  • OfStack

Forget the mongodb installation: centos yum installs mongodb and the php extension

1. Create, switch and delete the database


[root@localhost zhangy]# mongo 
MongoDB shell version: 2.4.6 
connecting to: tank 
> use test      // create  or  Switch database  
switched to db test 
> db.dropDatabase()  // Delete database  
{ "dropped" : "test", "ok" : 1 } 

2. php creates, switches and deletes databases

1. Switch database


$mongo = new Mongo(); 
$db = $mongo->selectDB('test');  // Switch database  

2. Create a database


$mongo = new Mongo(); 
$db = $mongo->selectDB('test'); 
$users = $db->createCollection("users"); 
 
$alldb = $mongo->listDBs(); // List all databases  
print_r($alldb);      // You can see db It's created  

Note 1 here that you can't create a database without creating an collection.

Delete the database


$mongo = new Mongo(); 
$db = $mongo->selectDB('test'); 
$db->drop(); 

3, section

This article is very simple, ha ha, do not want to write too much in an article, fold open write, see more clearly 1 point, more thin 1 point.
Here's the help from the mongodb command, which is very helpful for command line operations.

1, db help


db.AddUser(username,password)  Add user  
db.auth(usrename,password)    Set up database connection validation  
db.cloneDataBase(fromhost)    Clone from the target server 1 A database  
db.commandHelp(name)      returns the help for the command 
db.copyDatabase(fromdb,todb,fromhost)  Replication database fromdb--- Source database name, todb--- Target database name, fromhost--- Source database server address  
db.createCollection(name,{size:3333,capped:333,max:88888})  create 1 Is equal to 1 A table  
db.currentOp()          Cancels the current operation of the current library  
db.dropDataBase()        Delete the current database  
db.eval(func,args)       run code server-side 
db.getCollection(cname)     achieve 1 Data sets, with the same usage: db['cname'] or 
db.getCollenctionNames()     Gets a list of names for all data sets  
db.getLastError()        Returns the last 1 Error message  
db.getLastErrorObj()       Returns the last 1 The wrong object  
db.getMongo()          Gets the connection object for the current server get the server 
db.getMondo().setSlaveOk()   allow this connection to read from then nonmaster membr of a replica pair 
db.getName()           Returns the name of the database when the operation is performed  
db.getPrevError()        On the back 1 10 error objects  
db.getProfilingLevel()      To obtain profile level 
db.getReplicationInfo()     Get duplicate data  
db.getSisterDB(name)      get the db at the same server as this onew 
db.killOp()           Stops (kills) the current operation in the current library  
db.printCollectionStats()    Returns the dataset status of the current library  
db.printReplicationInfo()     Prints replication status information for the master database  
db.printSlaveReplicationInfo()     Print the copy status information from the database  
db.printShardingStatus()     Returns whether the current database is a Shared database  
db.removeUser(username)     Delete user  
db.repairDatabase()       Fix the current database  
db.resetError() 
db.runCommand(cmdObj)     run a database command. if cmdObj is a string, turns it into {cmdObj:1} 
db.setProfilingLevel(level)   Set up the profile level 0=off,1=slow,2=all 
db.shutdownServer()       Close the current service routine  
db.version()           Returns version information for the current program  

2. Table help, format, db. Table name.help()


db.test.find({id:10})      return test The data set ID=10 The data set  
db.test.find({id:10}).count()  return test The data set ID=10 Total number of data  
db.test.find({id:10}).limit(2)  return test The data set ID=10 The data set is from no 2 The data set at the beginning of the bar  
db.test.find({id:10}).skip(8)  return test The data set ID=10 The data set from 0 To the first 8 Data set of bar  
db.test.find({id:10}).limit(2).skip(8)  return test The data set ID=1= The data set is from no 2 To the first article 8 The data of  
db.test.find({id:10}).sort()   return test The data set ID=10 Sort data set of  
db.test.findOne([query])     Returns qualified 1 The data  
db.test.getDB()         Returns the database name to which this data set belongs  
db.test.getIndexes()       Returns index information for some data sets  
db.test.group({key:...,initial:...,reduce:...[,cond:...]})   Return group information  
db.test.mapReduce(mayFunction,reduceFunction,<optional params>)  This is kind of like a stored procedure  
db.test.remove(query)            Delete in the dataset 1 The data  
db.test.renameCollection(newName)      Rename some data sets  
db.test.save(obj)              Insert into the data set 1 The data  
db.test.stats()               Returns the state of this data set  
db.test.storageSize()            Returns the storage size of this dataset  
db.test.totalIndexSize()           Returns the index file size for this dataset  
db.test.totalSize()             Returns the total size of some data sets  
db.test.update(query,object[,upsert_bool])  Update in this dataset 1 The data  
db.test.validate()              Validate this data set  
db.test.getShardVersion()          Returns the Shared version number of the dataset  


Related articles: