MongoDB (4) Database & collection Operations

Source: Internet
Author: User
Tags mongodb
MongoDB Getting Started column

Http://blog.csdn.net/column/details/19681.html


Database Operations View Database

# View all databases under the current MONGO
> Show Databases
> Show DBS
Create a database
# Connect, create TestDB database
> Use TestDB
The TestDB database has been created at this time, but if you use the show DBS directive and cannot query the relevant information, this is because MongoDB does not display empty database, if you insert some data into the empty database TestDB, you can display the database; Deleting a database
# Delete TestDB Database
> Use TestDB
> Db.dropdatabase ()
Copy Databasecan use db.copydatabase (from_db,to_db [, From_host])to replicate the database;
# copy TestDB library to Testdb_copy Library
> Use TestDB
> db.copydatabase ("TestDB", "testdb_copy")
Renaming a databaseMongoDB does not provide instructions to rename the database, you can copy the database to achieve the renaming of the database;
# rename TestDB to testdb233
> Use TestDB
> db.copydatabase (' TestDB ', ' testdb233 ')
> Db.dropdatabase ()
But in this way, if the database is large and resource-intensive, you can use traversal to rename the collection:
# rename TestDB to testdb233
> Db.runcommand ({renamecollection: ' Testdb.orders ', to: ' Testdb233.orders '})


Collection Operations View Collections
# Displays all collections of the specified database
> Use TestDB
> Show Collections # or Show tables
Create a collection
db.createcollection (name,option)Used to create the collection, where the option parameter is as follows: Autoindexid: If the index is automatically created in the _id field, the default is false;
Capped: Whether to create a fixed collection, when specified as True when the collection capacity to the maximum value, automatically overwrites the oldest document, when specifying capped, you must specify the size parameter;
Size: Specifies the upper limit of the size of the fixed collection, in bytes;
Max: Specifies the maximum number of documents for a fixed collection;
※ when inserting a document, MongoDB checks the size first and then checks Max;
> Use TestDB
> db.createcollection (' articles ')
> db.createcollection (' site ', {capped:true, autoindexid:true, size:233300, max:10000})
In fact MongoDB does not need to create a collection, when inserting a document, MongoDB will automatically create a collection; Delete Collection
# Delete TestDB's site collection
> Use TestDB
> Db.site.drop ()
Renaming Collections
> Use TestDB
> db.site.renameCollection (' site233 ')

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.