Create, switch, and delete mongodb Databases

Source: Internet
Author: User
Mongodb is the most relational database in nosql. Single Table operations are basically the same as relational databases. Mongodb is easy to learn and easy to use. It can be recorded in several phases to learn and use mongodb. For more information about mongodb installation, see centosyum to install mongodb and php extension 1, create, switch, and delete data.

Mongodb is the most relational database in nosql. Single Table operations are basically the same as relational databases. Mongodb is easy to learn and easy to use. It can be recorded in several phases to learn and use mongodb. For more information about mongodb installation, see centos yum to install mongodb and php extension 1, create, switch, and delete data.

Mongodb is the most relational database in nosql. Single Table operations are basically the same as relational databases. Mongodb is easy to learn and easy to use. It can be recorded in several phases to learn and use mongodb. For more information about mongodb installation, see:Install mongodb and php extensions in centos yum

1. Create, switch, and delete Databases

[Root @ localhost zhangy] # mongoMongoDB shell version: 2.4.6connecting to: tank> use test // create or switch database switched to db test> db. dropDatabase () // Delete the database {"dropped": "test", "OK": 1}

2. Create, switch, and delete a database in php

1. Switch Database

$ Mongo = new Mongo (); $ db = $ mongo-> selectDB ('test'); // switch the 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 that the database has been created successfully.

Note that if you do not create a collection (the tables in the root relational database are basically the same), you cannot create a database.

3. delete a database

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

Section 3

This article is very simple. Haha, I don't want to write too many things in an article. I want to write them in a more clear and detailed manner.

Here I would like to mention the help of the mongodb command, which is very helpful for command line operations.

1. db help

Db. addUser (username, password) to add user db. auth (usrename, password) sets database connection verification db. cloneDataBase (fromhost) clones a database from the target server. commandHelp (name) returns the help for the commanddb. copy database (fromdb, todb, fromhost) Copy database fromdb --- source database name, todb --- target database name, fromhost --- source database server address db. createCollection (name, {size: 3333, capped: 333, max: 88888}) creates a dataset, which is equivalent to a table db. currentOp () cancels the current database operation db. dropDataBase () deletes the current database. eval (func, args) run code server-sidedb.getCollection (cname) gets a data set, the same usage: db ['cname'] ordb. getCollenctionNames () obtains the name list db of all data sets. getLastError () returns the message db for the last error. getlasterrobj () returns the last error object db. getMongo () gets the connection object of the current Server get the serverdb. getMondo (). setSlaveOk () allow this connection to read from then nonmaster membr of a replica pairdb. getName () returns the name db of the database to be operated. getPrevError () returns the last error object db. getProfilingLevel () Get profile leveldb. getReplicationInfo () to obtain duplicate data db. getSisterDB (name) get the db at the same server as this onewdb. killOp () stops (kills) the current operation db in the current database. printCollectionStats () returns the dataset status db of the current database. printReplicationInfo () prints the replication status information of the master database. printSlaveReplicationInfo () prints the copy status information of the slave database db. printShardingStatus () returns whether the current database is a shared database. removeUser (username) deletes the user database. repairDatabase () repairs the current database. resetError () db. runCommand (cmdObj) run a database command. if cmdObj is a string, turns it into {cmdObj: 1} db. setProfilingLevel (level) sets profile level 0 = off, 1 = slow, 2 = alldb. shutdownServer () closes the current service program db. version () returns the version information of the current program.

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

Db. test. find ({id: 10}) returns the database of the test dataset ID = 10. test. find ({id: 10 }). count () returns the total number of data db with the test dataset ID = 10. test. find ({id: 10 }). limit (2) returns the database of the dataset whose test dataset ID = 10 starts from the second one. test. find ({id: 10 }). skip (8) returns the db of the data set from 0 to the eighth with the test dataset ID = 10. test. find ({id: 10 }). limit (2 ). skip (8) returns the data db from the second to the eighth in the test dataset ID = 1 =. test. find ({id: 10 }). sort () returns the db of the sorting dataset with the test dataset ID = 10. test. findOne ([query]) returns a Qualified Data db. test. getDB () returns the database name db to which the dataset belongs. test. getIndexes () returns the index information db of some datasets. test. group ({key :..., initial :..., reduce :... [, cond:...]}) returns the group information db. test. mapReduce (mayFunction, performancefunction,) is a bit like a stored procedure db. test. remove (query) deletes a data db in the dataset. test. renameCollection (newName) rename some dataset names db. test. save (obj) inserts a data db into the dataset. test. stats () returns the status db for this dataset. test. storageSize () returns the storage size db for this dataset. test. totalIndexSize () returns the index file size db for this dataset. test. totalSize () returns the total size of some datasets in db. test. update (query, object [, upsert_bool]) updates a data db in this dataset. test. validate () verifies this dataset db. test. getShardVersion () returns the dataset share version number.

Original article address: Create, switch, and delete mongodb databases. Thank you for sharing it with the original author.

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.