怎么创建MongoDB数据库
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.
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.
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
2009-04-03 javascript使用右键事件