


A 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!

MongoDB is suitable for handling large-scale unstructured data, and Oracle is suitable for enterprise-level applications that require transaction consistency. 1.MongoDB provides flexibility and high performance, suitable for processing user behavior data. 2. Oracle is known for its stability and powerful functions and is suitable for financial systems. 3.MongoDB uses document models, and Oracle uses relational models. 4.MongoDB is suitable for social media applications, while Oracle is suitable for enterprise-level applications.

MongoDB's scalability and performance considerations include horizontal scaling, vertical scaling, and performance optimization. 1. Horizontal expansion is achieved through sharding technology to improve system capacity. 2. Vertical expansion improves performance by increasing hardware resources. 3. Performance optimization is achieved through rational design of indexes and optimized query strategies.

MongoDB is a NoSQL database because of its flexibility and scalability are very important in modern data management. It uses document storage, is suitable for processing large-scale, variable data, and provides powerful query and indexing capabilities.

You can use the following methods to delete documents in MongoDB: 1. The $in operator specifies the list of documents to be deleted; 2. The regular expression matches documents that meet the criteria; 3. The $exists operator deletes documents with the specified fields; 4. The find() and remove() methods first get and then delete the document. Please note that these operations cannot use transactions and may delete all matching documents, so be careful when using them.

To set up a MongoDB database, you can use the command line (use and db.createCollection()) or the mongo shell (mongo, use and db.createCollection()). Other setting options include viewing database (show dbs), viewing collections (show collections), deleting database (db.dropDatabase()), deleting collections (db.<collection_name>.drop()), inserting documents (db.<collecti

Deploying a MongoDB cluster is divided into five steps: deploying the primary node, deploying the secondary node, adding the secondary node, configuring replication, and verifying the cluster. Including installing MongoDB software, creating data directories, starting MongoDB instances, initializing replication sets, adding secondary nodes, enabling replica set features, configuring voting rights, and verifying cluster status and data replication.

MongoDB is widely used in the following scenarios: Document storage: manages structured and unstructured data such as user information, content, product catalogs, etc. Real-time analysis: Quickly query and analyze real-time data such as logs, monitoring dashboard displays, etc. Social Media: Manage user relationship maps, activity streams, and messaging. Internet of Things: Process massive time series data such as device monitoring, data collection and remote management. Mobile applications: As a backend database, synchronize mobile device data, provide offline storage, etc. Other areas: diversified scenarios such as e-commerce, healthcare, financial services and game development.

How to view MongoDB version: Command line: Use the db.version() command. Programming language driver: Python: print(client.server_info()["version"])Node.js: db.command({ version: 1 }, (err, result) => { console.log(result.version); });


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor