search
HomeDatabaseMongoDBA popular explanation of the concepts of MongoDB databases, collections, and documents

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

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

> db
huoying

To view all current databases, use show dbs;

> show dbs;
admin    0.000GB
config   0.000GB
huoying  0.000GB
local    0.000GB

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}

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 }

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}

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 }
>

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 }

Delete document

Use remove to delete document

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

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
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
MongoDB's Purpose: Flexible Data Storage and ManagementMongoDB's Purpose: Flexible Data Storage and ManagementMay 09, 2025 am 12:20 AM

MongoDB's flexibility is reflected in: 1) able to store data in any structure, 2) use BSON format, and 3) support complex query and aggregation operations. This flexibility makes it perform well when dealing with variable data structures and is a powerful tool for modern application development.

MongoDB vs. Oracle: Licensing, Features, and BenefitsMongoDB vs. Oracle: Licensing, Features, and BenefitsMay 08, 2025 am 12:18 AM

MongoDB is suitable for processing large-scale unstructured data and adopts an open source license; Oracle is suitable for complex commercial transactions and adopts a commercial license. 1.MongoDB provides flexible document models and scalability across the board, suitable for big data processing. 2. Oracle provides powerful ACID transaction support and enterprise-level capabilities, suitable for complex analytical workloads. Data type, budget and technical resources need to be considered when choosing.

MongoDB vs. Oracle: Exploring NoSQL and Relational ApproachesMongoDB vs. Oracle: Exploring NoSQL and Relational ApproachesMay 07, 2025 am 12:02 AM

In different application scenarios, choosing MongoDB or Oracle depends on specific needs: 1) If you need to process a large amount of unstructured data and do not have high requirements for data consistency, choose MongoDB; 2) If you need strict data consistency and complex queries, choose Oracle.

The Truth About MongoDB's Current SituationThe Truth About MongoDB's Current SituationMay 06, 2025 am 12:10 AM

MongoDB's current performance depends on the specific usage scenario and requirements. 1) In e-commerce platforms, MongoDB is suitable for storing product information and user data, but may face consistency problems when processing orders. 2) In the content management system, MongoDB is convenient for storing articles and comments, but it requires sharding technology when processing large amounts of data.

MongoDB vs. Oracle: Document Databases vs. Relational DatabasesMongoDB vs. Oracle: Document Databases vs. Relational DatabasesMay 05, 2025 am 12:04 AM

Introduction In the modern world of data management, choosing the right database system is crucial for any project. We often face a choice: should we choose a document-based database like MongoDB, or a relational database like Oracle? Today I will take you into the depth of the differences between MongoDB and Oracle, help you understand their pros and cons, and share my experience using them in real projects. This article will take you to start with basic knowledge and gradually deepen the core features, usage scenarios and performance performance of these two types of databases. Whether you are a new data manager or an experienced database administrator, after reading this article, you will be on how to choose and use MongoDB or Ora in your project

What's Happening with MongoDB? Exploring the FactsWhat's Happening with MongoDB? Exploring the FactsMay 04, 2025 am 12:15 AM

MongoDB is still a powerful database solution. 1) It is known for its flexibility and scalability and is suitable for storing complex data structures. 2) Through reasonable indexing and query optimization, its performance can be improved. 3) Using aggregation framework and sharding technology, MongoDB applications can be further optimized and extended.

Is MongoDB Doomed? Dispelling the MythsIs MongoDB Doomed? Dispelling the MythsMay 03, 2025 am 12:06 AM

MongoDB is not destined to decline. 1) Its advantage lies in its flexibility and scalability, which is suitable for processing complex data structures and large-scale data. 2) Disadvantages include high memory usage and late introduction of ACID transaction support. 3) Despite doubts about performance and transaction support, MongoDB is still a powerful database solution driven by technological improvements and market demand.

The Future of MongoDB: A Look at its ProspectsThe Future of MongoDB: A Look at its ProspectsMay 02, 2025 am 12:08 AM

MongoDB'sfutureispromisingwithgrowthincloudintegration,real-timedataprocessing,andAI/MLapplications,thoughitfaceschallengesincompetition,performance,security,andeaseofuse.1)CloudintegrationviaMongoDBAtlaswillseeenhancementslikeserverlessinstancesandm

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version