MongoDB didn’t provides any command to create “database“. Actually, you don’t need to create it manually, because, MangoDB will 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 you want to create, when first time you save the value into the defined collection (table), under selected database, MangoDB will create the value, collection and database automatically.

Note 
MongoDB contains “db.createCollection()” to create collection manually, but Not database.

In this guide, we will show you how and when MongoDB will create the database and collections.

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.

怎么创建MongoDB数据库的更多相关文章

  1. 【转载】CentOS6.5_X64下安装配置MongoDB数据库

    [转载]CentOS6.5_X64下安装配置MongoDB数据库 2014-05-16 10:07:09|  分类: 默认分类|举报|字号 订阅      下载LOFTER客户端 本文转载自zhm&l ...

  2. CentOS 6.5_X64下安装MongoDB数据库

    1.下载MongoDB 官网下载地址:https://www.mongodb.org/downloads 具体链接:https://fastdl.mongodb.org/linux/mongodb-l ...

  3. NodeJS连接MongoDB数据库时报错

    今天第一次尝试连接MongoDB数据库,具体步骤也很简单. 首先,通过NodeJS运行环境安装MongoDB包,进入要安装的目录,执行语句 npm install mongodb 安装成功后,通过如下 ...

  4. 安装配置MongoDB数据库

    一.关闭SElinux.配置防火墙 1.vi /etc/selinux/config #SELINUX=enforcing #注释掉 #SELINUXTYPE=targeted #注释掉 SELINU ...

  5. Linux下定时切割Mongodb数据库日志并删除指定天数前的日志记录

    此为在网络上找来的,觉得很好! 实现目的: 对Mongodb数据库日志按天保存,并且只保留最近7天的日志记录. 具体操作: 使用Mongodb数据库自带的命令来切割日志 ps -def | grep ...

  6. 入门系列之在Ubuntu 14.04上备份,还原和迁移MongoDB数据库

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由信姜缘 发表于云+社区专栏 MongoDB是最受欢迎的NoSQL数据库引擎之一.它以可扩展,强大,可靠和易于使用而闻名.在本文中,我们 ...

  7. Linux下安装配置MongoDB数据库

    说明: 操作系统:CentOS 5.X 64位 IP地址:192.168.21.130 实现目的: 安装配置MongoDB数据库 具体操作: 一.关闭SElinux.配置防火墙 1.vi /etc/s ...

  8. java操作mongodb数据库实现新建数据库,新建集合,新建文档

    *首先明确一点,要通过java代码创建mongodb数据库实例,需要同时创建集合和文档. 代码实现: /* 建立与mongodb数据库的连接,可指定参数,如:MongoClient client = ...

  9. 用shell脚本实现MongoDB数据库自动备份

    一.创建MongoDB备份目录 用来存放数据 mkdir -p /data/mongodb_bak/mongodb_bak_now mkdir -p /data/mongodb_bak/mongodb ...

随机推荐

  1. MessagePack, Protocol Buffers和Thrift序列化框架原理和比较说明

    MessagePack, Protocol Buffers和Thrift序列化框架原理和比较说明 http://www.open-open.com/lib/view/open1412731170858 ...

  2. PHP开发工具介绍之zendStudio

    1.PHP开发工具介绍之zendStudio 下载:进入官网:http://www.zend.com/en/products/studio 选择下载安装 注意这里的工作空间要和你Apache的工作目录 ...

  3. winform自定义文件程序-- 不允许所请求的注册表访问权(ZSSQL)

    常见问题1: 不允许所请求的注册表访问权 win7.win8 双击程序文件ZSSQL时候会出现 不允许所请求的注册表访问权 的弹窗异常 解决方法:ZSSQL.exe 右键 属性--兼容性--以管理员身 ...

  4. 使用typeid(变量或类型).name()来获取常量或变量的类型---gyy整理

    使用typeid(变量或类型).name()来获取常量或变量的类型 <typeinfo>  该头文件包含运行时类型识别(在执行时确定数据类型)的类 typeid的使用   typeid操作 ...

  5. MyBatis的动态SQL操作--插入

    需求:向数据库中插入一条数据 //id,name,sal非空,三个字段都插入 insert into student(id,name,sal) values (?,?,?) //id,name非空,只 ...

  6. 面试常考的数据结构Java实现

    1.线性表 2.线性链表 3.栈 4.队列 5.串 6.数组 7.广义表 8.树和二叉树 的结点),并且,二叉树的子树有左右之分,其次序不能任意颠倒. 二叉树的性质: :在二叉树的第 i 层上至多有2 ...

  7. 列出man手册所有函数的方法

    locate /man7/|sed -r 's#.*/([^/]+).7.gz$#\1#' locate /man7/ | xargs basename -a -s '.7.gz' apropos - ...

  8. linux 查看系统信息命令

    linux 查看系统信息命令是linux初学者必备的基础知识, 这些命令也非常有用, 因为进入linux第一件事就可能是首先查看系统信息, 因此必要的系统的学习一下这些linux系统信息命令还是非常有 ...

  9. 函数hash_get_nth_cell

    /************************************************************//** Gets the nth cell in a hash table. ...

  10. [转]Linux下Nagios的安装与配置

    转自:http://blog.chinaunix.net/uid-29539073-id-4149856.html 月色书香 一.Nagios简介 Nagios是一款开源的电脑系统和网络监视工具,能有 ...