博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
怎么创建MongoDB数据库
阅读量:6836 次
发布时间:2019-06-26

本文共 1864 字,大约阅读时间需要 6 分钟。

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.1connecting to: test> show dbsadmin   0.03125GBlocal   (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 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) ‘{username:"mkyong"}‘ into the ‘user’ collection”. When this “save” operation is performed, MangoDB will create the “user” collection, and “mkyongdb” database automatically.

转载地址:http://fuqkl.baihongyu.com/

你可能感兴趣的文章
超链接浏览<meta name="format-detection"/> 的用法
查看>>
请求网络网络编程
查看>>
文件目录Android SDK目录结构
查看>>
Asp.net Web.Config - 配置元素customErrors
查看>>
Android: how to resolve Application’s parameter NullPointerException
查看>>
EntityFramework用法探索(二)CodeFirst
查看>>
人人都来写算法 之 快速排序
查看>>
[转]SQLServer和Oracle,存储过程区别,常用函数对比
查看>>
如何在ArcMap中监听键盘鼠标事件
查看>>
vs2012中程序集生成无法自动在网站Bin目录下生成Dll文件?(已解决!)
查看>>
fastDFS同步问题讨论
查看>>
ActiveMQ学习笔记(二) JMS与Spring
查看>>
实验室报告:VMware vSphere Data Protection
查看>>
php的数组与字符串的转换函数整理
查看>>
WCF 框架运行时类图
查看>>
spring配置异步执行
查看>>
软件开发报价的计算方法
查看>>
大型网站系统架构分析--转
查看>>
php 文件操作
查看>>
poj 1474 Video Surveillance - 求多边形有没有核
查看>>