Home > Database > MongoDB > body text

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

齐天大圣
Release: 2020-08-19 11:32:52
Original
3700 people have browsed it

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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template