[MongoDB] This article takes you to quickly master the basic operations of MongoDB database and collection


foreword

In order to consolidate the knowledge learned, the author tried to start publishing some blogs of learning notes for future review. Of course, it would be great if it could help some newcomers learn new technologies. The author is a piece of food. If there are any mistakes in the records in the article, readers and friends are welcome to criticize and correct.
(The reference source code of the blog can be found in the resources on my homepage. If you have any questions during the learning process, please feel free to ask me in the comment area)

1. Case requirements

The data for storing article comments is stored in MongoDB. The data structure reference is as follows:

Database: articledb

Op-Ed Comments comment
Field Name field meaning Field Type Remark
_id ID ObjectId或String Mongo's primary key field
articleid Article ID String
content comments String
userid Reviewer ID String
nickname Reviewer Nickname String
createdatetime datetime of the comment Date
likenum Likes Int32
replynum Replies Int32
state Status String 0: Invisible; 1: Visible;
parentid Superior ID String If 0 means the article's top comment

2. Database operation

1. Select and create a database

  • Syntax for selecting and creating a database:

use database name

  • If the database does not exist, it will be created automatically. For example, the following statement creates the spitdb database:

use articledb

  • View all database commands that you have permission to view

show dbs

show databases

insert image description here

  • In addition: the database name can be any UTF-8 string that meets the following conditions.
  1. Cannot be an empty string ("")
  2. Must not contain ' ' (space), ., $, /, \ and \0 (null characters)
  3. should be all lowercase
  4. up to 64 bytes
  • Some database names are reserved, and these databases with special functions can be accessed directly.
  1. admin: From a permissions standpoint, this is the "root" database. If a user is added to this database, the user automatically inherits all database permissions. Some specific server-side commands can also only be run from this database, such as listing all databases or shutting down the server
  2. local: this data is never replicated and can be used to store arbitrary collections limited to a single local server
  3. config: When Mongo is used for sharding settings, the config database is used internally to save information about sharding

注意: 在 MongoDB 中,集合只有在内容插入后才会创建! 就是说,创建集合(数据表)后要再插入一个文档(记录),集合才会真正创建

2. Delete the database

  • The syntax format of MongoDB to delete the database is as follows

db.dropDatabase()

insert image description here

提示:主要用来删除已经持久化的数据库

3. Collection operations

A collection, similar to a table in a relational database. Can be created explicitly or implicitly

1. Explicit creation of collections (understanding)

  • Basic syntax format:

db.createCollection(name)

  • Parameter Description:
  1. name: the collection name to create
  2. For example: create a normal collection called mycollection
    db.createCollection("mycollection")

insert image description here

  • View the tables in the current library: show tables command

show collections

show tables

insert image description here

  • Collection naming convention:
  1. Collection name cannot be an empty string ""
  2. The collection name cannot contain the \0 character (null character), this character indicates the end of the collection name
  3. Collection names cannot start with "system.", this is a reserved prefix for system collections
  4. User-created collection names cannot contain reserved characters. Some drivers do support inclusion in collection names because some system-generated collections include this character. Unless you're accessing collections created by such a system, never include $ in the name

2. Implicit creation of collections

When inserting a document into a collection, the collection is automatically created if it does not exist.

提示:通常我们使用隐式创建文档即可

3. Collection deletion

  • The syntax for collection deletion is as follows:

db.collection.drop()
or
db.collection.drop()

  • return value

The drop() method returns true if the selected collection is dropped successfully, otherwise it returns false. For example: To delete the mycollection collection
db.mycollection.drop()

insert image description here

Summarize

Everyone is welcome to leave a message for exchange and criticism. If the article is helpful to you or you think the author's writing is not bad, you can click to follow, like, and bookmark to support.
(The reference source code of the blog can be found in the resources on my homepage. If you have any questions during the learning process, please feel free to ask me in the comment area)

Guess you like

Origin blog.csdn.net/HHX_01/article/details/132257543