MongoDB database & amp; Set Operations,

Source: Internet
Author: User

MongoDB database & set operations,
Database Operations to view databases?

# View all databases in the current mongo
> show databases ?
> show dbs
Create a database ??
# Connect to create a testdb Database
> use testdb
At this time, the testdb database has been created. However, if the show dbs command is used, the related information cannot be queried. This is because mongodb does not display empty databases. If some data is inserted into testdb, the database is displayed. Delete the database?
# Deleting a testdb Database
> use testdb
> db.dropDatabase()
Copy a database Db. copyDatabase (from_db, to_db [, from_host])To copy the database ;?
# Copy the testdb database to the testdb_copy Database
> use testdb
> db.copyDatabase("testdb", "testdb_copy")
Mongodb does not provide commands to rename a database. You can rename a database by copying the database ;?
# Rename testdb to testdb233
> use testdb
> db.copyDatabase('testdb', 'testdb233')
> db.dropDatabase()
However, if the database is large enough to occupy resources, you can use traversal to rename the set :?
# Rename testdb to testdb233
> use testdb 
> db.runCommand( { renameCollection:'testdb.orders', to: 'testdb233.orders' } )


Set operation view set?
# Display all sets of the specified database
> use testdb
> Show collections? # Or show tables
Create a set Db. createCollection (name, option)Used to create a set. The optional options are as follows: AutoIndexID: whether to automatically create an index in the _ id field. The default value is false;
Capped: whether to create a fixed set. If it is set to true, when the set capacity reaches the maximum value, the earliest documents will be automatically overwritten. When capped is specified, the size must be specified? Parameters;
Size: specifies the maximum size of a fixed set, in bytes;
Max: specifies the maximum number of documents in a fixed set;
※When inserting a document, mongodb first checks the size and then max ;?
> use testdb
> db.createCollection('articles')
> db.createCollection( 'site', { capped:true, autoIndexID:true, size:233300, max: 10000} )
Actually mongodb? You do not need to create a set. When you insert a document, mongodb will automatically create a set and delete the set?
# Delete the site set of testdb
> use testdb
> db.site.drop()
Rename a set?
# Rename the site set of the testdb database to site233
> 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.