How to create a MongoDB database

Source: Internet
Author: User

MongoDB didn ' t provides any command to create "database". Actually, you don ' t need to create it manually, because, Mangodb'll create it on the fly, during the first time you save The value into the defined collection (or table in SQL), and database.

For developer from SQL background, we need to create a database, table and insert values into table manually. In MongoDB, you don ' t need to mention "What do you want to create, when first time you save the value into the defined collect Ion (table), under selected database, Mangodb would create the value, collection and database automatically.

Note
MongoDB contains " db.createCollection()"To create collection manually, and not database.

In this guide, we'll show you what and when MongoDB would create the database and collections.

1. Show All database

Issue "to the show dbs display all available databases.

MongoDB Shell version:1.8.1connecting to:test> show Dbsadmin   0.03125GBlocal   (empty)

Currently, only databases is available– "admin" and "local".

2. Define a database name

Issue "To switch from the use new-databasename default database to define database (even non-exists database name would work). However, Mangodb doesn ' t create any database yet, until you save something inside.

> Use mkyongdbswitched to DB mkyongdb> show Dbsadmin   0.03125GBlocal   (empty)

P.S Database "mkyongdb" is not created yet.

3. Save It

Define A collection named "Users", and save a dummy document (value) inside.

> db.users.save ({username: "Mkyong"})> Db.users.find () {"_id": ObjectId ("4dbac7bfea37068bd0987573"), "username": "Mkyong"}>> show Dbsadmin   0.03125GBlocal   (empty)mkyongdb        0.03125GB

This says, "Save this document (value) ' To {username:"mkyong"} the ' user ' collection". When this "save" operation is performed, Mangodb'll create the "user" collection, and "mkyongdb" Datab ASE automatically.

How to create a MongoDB database

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.