Home Database MongoDB A popular explanation of the concepts of MongoDB databases, collections, and documents

A popular explanation of the concepts of MongoDB databases, collections, and documents

Aug 19, 2020 am 10:27 AM
mongodb

For relational databases, we know that there are multiple tables in the database and multiple rows of data in the data tables. For MongoDB, there are multiple collections in the database, and the collections contain multiple documents.

Database

The database concept of MongoDB is similar to that of Mysql. Each database can set independent permissions.

First, let’s look at how to create a database. MongoDB also has restrictions on database names. Only numbers or letters can be used. In addition, database names are case-sensitive. Generally, we will use lowercase. In addition, some keywords cannot be used, such as: admin, config, local, etc.

In MongoDB, there is no need to create a database explicitly. You only need to select use dbName. When the database does not exist, it will be automatically created for us.

> use huoying
switched to db huoying
Copy after login

When using the db command, the currently used database name will be displayed.

> db
huoying
Copy after login

To view all current databases, use show dbs;

> show dbs;
admin    0.000GB
config   0.000GB
huoying  0.000GB
local    0.000GB
Copy after login

Collection

MongoDB’s collection is similar to the table in Mysql. How many collections are there? consists of documents. But collections do not have field restrictions like tables, and the documents under the collection can be of various types. For example, a collection may have the following two types of documents:

{"name":"gwx", "age" : 30},
{"id":1, "score":100}
Copy after login

Although collections have no restrictions on documents, for the convenience of data management, we generally put related documents in a collection.

Creating a collection is divided into direct creation and implicit creation.

# 显示的创建集合
> db.createCollection('huoying')
{ "ok" : 1 }
Copy after login

Implicitly create a collection. When creating a document directly, if the collection does not exist, the collection will be created first

Document

Finally, let’s take a look at the documentation. The data type of a document is similar to associated data in PHP or objects in JavaScript.

Note that documents are case and data type sensitive. Look at the following example:

# 下面两个是不一样的文档,他们的数据类型不一样
{"name":30}
{"name":"30"}

# 下面两个是不一样的文档,键名不一样
{"name":30}
{"NAME":30}
Copy after login

New document

Use the command db.collection name.insertOne (document data) to insert into the specified collection A document. If the collection does not exist, it will be created implicitly.

> db.users.insertOne({"name":"gwx", "age":29})
{
 "acknowledged" : true,
 "insertedId" : ObjectId("5f0cfdbb7688816db4d031bc")
}
> db.users.find()
{ "_id" : ObjectId("5f0cfdbb7688816db4d031bc"), "name" : "gwx", "age" : 29 }
>
Copy after login

Modify the document

Use update to update the document content. update requires two parameters. The first one is the qualification. Used to find the specified document, the second is the content of the new document.

> db.users.update({name:"gwx"}, {name:"monkeyking", age:500})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.users.find()
{ "_id" : ObjectId("5f0cfdbb7688816db4d031bc"), "name" : "monkeyking", "age" : 500 }
Copy after login

Delete document

Use remove to delete document

> db.users.remove({age:500})
WriteResult({ "nRemoved" : 1 })
Copy after login

The above is the detailed content of A popular explanation of the concepts of MongoDB databases, collections, and documents. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What to do if navicat expires What to do if navicat expires Apr 23, 2024 pm 12:12 PM

Solutions to resolve Navicat expiration issues include: renew the license; uninstall and reinstall; disable automatic updates; use Navicat Premium Essentials free version; contact Navicat customer support.

How to connect navicat to mongodb How to connect navicat to mongodb Apr 24, 2024 am 11:27 AM

To connect to MongoDB using Navicat, you need to: Install Navicat Create a MongoDB connection: a. Enter the connection name, host address and port b. Enter the authentication information (if required) Add an SSL certificate (if required) Verify the connection Save the connection

What is the use of net4.0 What is the use of net4.0 May 10, 2024 am 01:09 AM

.NET 4.0 is used to create a variety of applications and it provides application developers with rich features including: object-oriented programming, flexibility, powerful architecture, cloud computing integration, performance optimization, extensive libraries, security, Scalability, data access, and mobile development support.

Integration of Java functions and databases in serverless architecture Integration of Java functions and databases in serverless architecture Apr 28, 2024 am 08:57 AM

In a serverless architecture, Java functions can be integrated with the database to access and manipulate data in the database. Key steps include: creating Java functions, configuring environment variables, deploying functions, and testing functions. By following these steps, developers can build complex applications that seamlessly access data stored in databases.

How to configure MongoDB automatic expansion on Debian How to configure MongoDB automatic expansion on Debian Apr 02, 2025 am 07:36 AM

This article introduces how to configure MongoDB on Debian system to achieve automatic expansion. The main steps include setting up the MongoDB replica set and disk space monitoring. 1. MongoDB installation First, make sure that MongoDB is installed on the Debian system. Install using the following command: sudoaptupdatesudoaptinstall-ymongodb-org 2. Configuring MongoDB replica set MongoDB replica set ensures high availability and data redundancy, which is the basis for achieving automatic capacity expansion. Start MongoDB service: sudosystemctlstartmongodsudosys

How to ensure high availability of MongoDB on Debian How to ensure high availability of MongoDB on Debian Apr 02, 2025 am 07:21 AM

This article describes how to build a highly available MongoDB database on a Debian system. We will explore multiple ways to ensure data security and services continue to operate. Key strategy: ReplicaSet: ReplicaSet: Use replicasets to achieve data redundancy and automatic failover. When a master node fails, the replica set will automatically elect a new master node to ensure the continuous availability of the service. Data backup and recovery: Regularly use the mongodump command to backup the database and formulate effective recovery strategies to deal with the risk of data loss. Monitoring and Alarms: Deploy monitoring tools (such as Prometheus, Grafana) to monitor the running status of MongoDB in real time, and

How to connect nodejs to database How to connect nodejs to database Apr 21, 2024 am 06:16 AM

To connect to the database, Node.js provides multiple database connector packages for MySQL, PostgreSQL, MongoDB, and Redis. The connection steps include: 1. Install the corresponding connector package; 2. Create a connection pool to maintain reusable connections; 3. Establish a connection with the database. Note: The operation is asynchronous and errors need to be handled to ensure security and optimize performance.

Can navicat connect to mongodb? Can navicat connect to mongodb? Apr 23, 2024 pm 05:15 PM

Yes, Navicat can connect to MongoDB database. Specific steps include: Open Navicat and create a new connection. Select the database type as MongoDB. Enter the MongoDB host address, port, and database name. Enter your MongoDB username and password (if required). Click the "Connect" button.

See all articles