MongoDB database operation-Create, switch, delete _mongodb

Source: Internet
Author: User
Tags mongodb

MongoDB installation will not say, please refer to: CentOS Yum installation mongodb and PHP extension

One, create, switch, delete database

[Root@localhost zhangy]# MONGO 
MongoDB shell version:2.4.6 connecting To:tank 
> Use test      //Create or toggle number According to the library 
switched to DB Test 
> Db.dropdatabase ()  ///delete database 
{"Dropped": "Test", "OK": 1} 

Second, PHP create, switch, delete database

1, switching the database

$mongo = new MONGO (); 
$db = $mongo->selectdb (' test ');  switching databases 

2, create the database

$mongo = new MONGO (); 
$db = $mongo->selectdb (' test '); 
$users = $db->createcollection ("users"); 
 
$alldb = $mongo->listdbs (); List all database 
Print_r ($ALLDB);      You can see that DB creation was successful 

Note here that if you do not create a collection (the table of the root relational database is essentially the same), you cannot create the database.

3, delete the database

$mongo = new MONGO (); 
$db = $mongo->selectdb (' test '); 
$db->drop (); 

Three, section

This article is very simple, haha, do not want to write in an article too many things, folded to write, look more clearly, a little more.
Here to mention the help under the MongoDB command, which is helpful for command-line operations.

1,db's Help

Db. AddUser (username,password) Add user Db.auth (Usrename,password) Set database connection authentication Db.clonedatabase (fromhost) clone a database from the target server db.co MMANDHELP (name) returns the Help for the command Db.copydatabase (fromdb,todb,fromhost) Copy database fromdb---Source database name, Todb---Mesh 
The name of the database, Fromhost---Source database server address db.createcollection (name,{size:3333,capped:333,max:88888}) to create a dataset, equivalent to a table Db.currentop () cancels the current operation of the current library db.dropdatabase () deletes the current database Db.eval (Func,args) Run code server-side DB.GETC Ollection (CNAME) obtains a collection of data, with the same usage: db[' cname '] or db.getcollenctionnames () gets the list of names for all data sets Db.getlasterror () returns the last The wrong prompt message db.getlasterrorobj () returns the object Db.getmongo () of the last error () Gets the connection object of the current server, 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 operation database Db.getp Reverror () returns the previous Error Object Db.getprofilinglevel () Gets the profile level Db.getreplicationinfo () to obtain 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 current library's dataset Status Db.printreplicationinfo () Print the replication status information for the primary database Db.printslavereplicationinfo () prints the replication status information from the database Db.printshardingstatus () returns whether the current database is a shared database Db.remove User (username) db.repairdatabase () to fix the current database Db.reseterror () Db.runcommand (cmdobj) run a database Comman D. If Cmdobj is a string, turns it to {cmdobj:1} db.setprofilinglevel (level) Set profile level 0=off,1=slow,2=all db.sh 
 Utdownserver () Closes the current service program Db.version () returns the version information of the current program

2, table help, Format, db. Table name. Aid ()

Db.test.find ({id:10}) returns the DataSet Db.test.find ({id:10}) of the test dataset id=10. Count () returns the total number of data id=10 ({Db.test.find}) for the test dataset Id:10. l Imit (2) returns the DataSet id=10 the test dataset Db.test.find ({id:10}) from the second. Skip (8) returns the DataSet id=10 from 0 to eighth of the dataset in the test DataSet Db.test.find ({ID 
: Limit (2). Skip (8) returns the data Db.test.find ({id:10}) from the second to eighth of the DataSet id=1= the test DataSet. Sort () returns the sorted dataset for the test dataset id=10 Db.test.findOne ([query]) returns a data Db.test.getDB () that matches the criteria () returns the database name Db.test.getIndexes () that the dataset belongs to and returns the index information DB for some datasets. Test.group ({key:...,initial:...,reduce: ...)  [, cond: ...]}) Returns the packet information Db.test.mapReduce (mayfunction,reducefunction,<optional params>) This is a bit like stored procedure db.test.remove (query) in              Data set Deletes a data db.test.renameCollection (NewName) renames some dataset name Db.test.save (obj) inserts a data into the DataSet Db.test.stats () Returns the state of this DataSet Db.test.storageSize () returns the storage size of this dataset Db.test.totalIndexSize () returns the index file size of this dataset db.tes  T.totalsize () returns the total size of some datasets Db.test.update (Query,object[,upsert_bool]) updates a data in this DataSet Db.test.validate ()           Verify that this dataset Db.test.getShardVersion () returns the dataset share version number 
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.