Mongo collection operations

2. Create a switch database

2.1 Default database

Like other types of databases, mongo databases can create databases, and multiple databases can be created.

The mongo database will have four databases by default, which are

  • admin: mainly stores information such as MongoDB users and roles

  • config: mainly stores the basic information of the shard cluster

  • local: mainly stores the metadata of the replica set

  • test: It will not be displayed when no data is inserted

    The test library is a default database, except for the test library, the admin, config, and local libraries are system libraries

2.2.1 admin library

The admin database is one of mong's default system libraries. It is mainly used to store information such as users, roles, and versions. There are two collections (called tables in other databases) by default under the admin library, which are role information and versions. information.

# 切换到admin库下
use admin
# 查看是否切换成功
db
#查看admin库中的集合
show collections

insert image description here

Among them, system.version mainly stores some featureCompatibilityVersion information and authSchema information

db.system.version.find()

insert image description here

system.users mainly stores some user role information
insert image description here

​ You can create any collection and store any data under the admin database, but it is strongly recommended not to use the admin database to store application business data, it is best to create a new database.
  The data of the system.users collection in the admin database will be cached in memory, so that user role information does not need to be loaded from disk every time authentication is performed.
  The lock level of the write operation of the MongoDB admin database can only reach the DB level, and does not support concurrent writing of multiple collections, nor does it support concurrent reading during writing. If users store business data in the admin database, they may encounter performance problems.

2.2.2 config library

The config database is also one of the default system libraries of mongo, which mainly stores the metadata of the replica set

# 切换到local库下
use local
# 查看是否切换成功
db
#查看local库中的集合,
show collections

2.2.3 local library

The local database is also one of the default system libraries of mongo, which is mainly used to store the information of the sharding cluster, and generally only works when sharding

# 切换到local库下
use local
# 查看是否切换成功
db
#查看local库中的集合,
show collections

2.2 Create switch delete database

2.2.1 Create switch database

The same command is used to create and switch databases in mongo

use database_name

If DataBase_Name exists, switch directly, if it does not exist, create directly

# 查看当前已存在的所有数据库
>show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
# 创建数据库
>use demo
switched to db demo
# 再次查看当前存在的所有数据库
>show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
# 可以发现,没有刚才自己创建的数据库,那是因为在mongo中创建的数据库默认不展示,需要想向数据库中插入数据,此时数据库才会展示
>db.demo.insert({
   
   "name":"hello worod"});
>>show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
demo    0.000GB

2.2.3 Delete database

#切换到需要删除的数据库
>use demo
#查看
>db
#删除
>db.dropDatabase()

2.2.4 Database naming convention

  1. Cannot be an empty string ("")
  2. Must not contain ' ' (space), ., $, /, \, and \0 (null character).
  3. Should be all lowercase, and up to 64 bytes
  4. Some database names are reserved and cannot be used as your own library name (for example: admin config local)

3. Set operation

In the mongo database, the data table is called a collection.

SQL terms/concepts MongoDB terms/concepts explain
database database database
table collection Database Table/Collection

3.1 Create collection

There are two main ways to create collections in mongo:

  1. Implicitly create collections
  2. show create collection

3.1 Implicit creation of collections

Implicitly create a collection, as the name implies, you have not created a collection, but mongo has already created a collection for you. In fact, the essence is that when we create a database, mongo will create a collection with the same name as the database by default.

use demo
db.demo.insert({id:1,name:"hello mongo"})

At this point, a default collection will be created, but if no data is inserted, the database will not be displayed.

3.2 Display creation collection

Display creation collection is the creation collection displayed by command

grammar:

​ db.createCollection(name, options)

Parameter Description:

  • name: the collection name to create
  • options: Optional parameter, specify options about memory size and index
field type describe
capped Boolean If true, a capped collection is created. A fixed collection is a collection with a fixed size that automatically overwrites the oldest documents when the maximum value is reached. When the value is true, the size parameter must be specified. , the default is false
size value Specify a maximum value, in bytes, for capped collections. If capped is true, this field also needs to be specified.
max value Specifies the maximum number of documents contained in a capped collection .

example:

#得到所有集合(不包含集合的详细信息)
db.getCollectionNames()
#得到集合的详细信息
db.getCollectionInfos()
#创建固定集合
db.createCollection("test1",{capped:true,size:1000,max:1000})
#创建非固定集合
db.createCollection("test")

insert image description here

3.2 View Collection

There are two main ways to view the collection

  1. View collection details
  2. View the collection, only get the name of the collection
#得到所有集合(不包含集合的详细信息)
db.getCollectionNames()
#得到集合的详细信息
db.getCollectionInfos()

![

3.3 Delete Collection

When we do not want to use a certain collection (table), or are not using a certain collection, in order to save space, we can delete a certain collection at this time, but we must pay special attention when deleting a collection. Generally, it is not recommended to delete a collection, especially is in the production environment.

grammar:

db.collection_Nane.drop()

If the deletion is successful, return true, otherwise return false

example:

#删除某个集合
db.test.drop()

insert image description here

NOTE : This is not recommended, especially in a production environment.

Guess you like

Origin blog.csdn.net/qq_40520912/article/details/130685411