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.