1. Show all database

Issue “show dbs” to display all available databases.

MongoDB shell version: 1.8.1
connecting to: test
> show dbs
admin   0.03125GB
local   (empty)

Currently, only two databases are available – “admin” and “local“.

2. Define a database name

Issue “use new-databasename” to switch from default database to define database (even non-exists database name will work). However, MangoDB doesn’t create any database yet, until you save something inside.

> use mkyongdb
switched to db mkyongdb
> show dbs
admin   0.03125GB
local   (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 dbs
admin   0.03125GB
local   (empty)
mkyongdb        0.03125GB

This says, “save this document (value) ‘{username:"mkyong"}‘ into the ‘user’ collection”. When this “save” operation is performed, MangoDB will create the “user” collection, and “mkyongdb” database automatically.

References

  1. SQL to MongoDB Mapping chart
Advertisements