How to create a new database and set in MongoDB

Source: Internet
Author: User
Because Mongodb is not a relational database file, in fact, it does not have the so-called ldquo in traditional relational databases; database rdquo; concept, but don't worry, when you

Because Mongodb is not a relational database file, in fact, it does not have the so-called ldquo in traditional relational databases; database rdquo; concept, but don't worry, when you

Create a Mongodb Database

Because Mongodb is not a relational database file, it does not actually have the so-called "Database" concept in traditional relational databases, but don't worry. When you add data for the first time, mongodb will be saved and created in the form of collection sets without manual creation. The following is an example:

1) list the current database

MongoDB shell version: 1.8.1

Connecting to: test

> Show dbs-

Admin 0.03125 GB

Local (empty)

You can use show dbs to list the number of databases currently. The preceding two databases are admin and local.

2) define a new database name

We use the "use new-databasename" syntax to use a new database. Note that this can be used even if your database has not been created, because mongodb will be created only after the data is actually inserted.

> Use mkyongdb

Switched to db mkyongdb

> Show dbs

Admin 0.03125 GB

Local (empty)

Note: After use mkyongdb, mkyongdb has not been actually created, but it indicates that mkyongdb is currently in use.

3) save data

Define a collection named "users" and insert data as follows:

> Db. users. save ({username: "mkyong "})

> Db. users. find ()

{"_ Id": ObjectId ("4dbac7bfea37068bd0987573"), "username": "mkyong "}

>

> Show dbs-

Admin 0.03125 GB

Local (empty)

Mkyongdb 0.03125 GB

You can see that you can use db. users. find () to find the inserted data. At this time, the collection named "users" has been created, and the database mkyongdb has also been created.

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.