当前位置:网站首页>Mongodb learning and sorting (basic command learning of users, databases, collections and documents)
Mongodb learning and sorting (basic command learning of users, databases, collections and documents)
2022-06-12 16:31:00 【JAVA·D·WangJing】
One 、 user
#### Create user
# roles: Specify the user's role , You can use an empty array to set an empty role for a new user ; stay roles Field , You can specify built-in roles and user-defined roles .role You can choose the role in :
# Built-In Roles( Built in characters ):
# 1、 Database user role :read、readWrite;
# 2、 Database management role :dbAdmin、dbOwner、userAdmin;
# 3、 The role of cluster management :clusterAdmin、clusterManager、clusterMonitor、hostManager;
# 4、 Backup recovery role :backup、restore;
# 5、 All database roles :readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase
# 6、 Super user role :root
# 7、 Internal roles :__system
# Specific roles :
# Read: Allows the user to read the specified database
# readWrite: Allows users to read and write to a specified database
# dbAdmin: Allows the user to execute administrative functions in the specified database , Like index creation 、 Delete , View statistics or access system.profile
# userAdmin: Allow the user to system.users A collection of written , You can find the specified database to create 、 Delete and manage users
# clusterAdmin: Only in admin Available in the database , Gives the user administrative rights to all sharding and copy-set related functions .
# readAnyDatabase: Only in admin Available in the database , Give the user read rights to all databases
# readWriteAnyDatabase: Only in admin Available in the database , Gives the user read and write access to all databases
# userAdminAnyDatabase: Only in admin Available in the database , That gives the user all the databases userAdmin jurisdiction
# dbAdminAnyDatabase: Only in admin Available in the database , That gives the user all the databases dbAdmin jurisdiction .
# root: Only in admin Available in the database . Super account , Super authority .
db.createUser({ user:'wangjing',pwd:'wangjing',roles:[{role:"userAdminAnyDatabase", db:"admin"}]});
#### Verify user information
db.auth('wangjing', 'wangjing')
#### Modify the information
### Change Password
db.changeUserPassword("wangjing", "wangjing1")
### Modify user information
db.updateUser("wangjing", {roles:[{role:"root", db:"admin"}]})
#### Delete user
### Delete user
db.dropUser("wangjing")
### Delete all users under the current data
db.dropAllUsers()
### View the users under the current data ( User management permission is required )
show users
Two 、 database
#### Create database
### establish 'wangjing_test' database
use wangjing_test
### View the current database
db
### View all databases (wangjing_test non-existent )
show dbs
### Insert a piece of data
db.wangjing_test.insert({"name":"wangjing"})
### Delete the current database
db.dropDatabase()
3、 ... and 、 aggregate
### Create set
# db.createCollection(name, options)
# Parameter description :
# name: The name of the collection to be created
# options: Optional parameters , Specifies the options for memory size and index
# options It could be the following argument :
# capped( Boolean ) ( Optional ) If true, Creates a fixed collection . A fixed set is a set with a fixed size , When it reaches its maximum , It automatically overwrites the earliest document . When the value is true when , Must specify size Parameters .
# autoIndexId( Boolean ) ( Optional ) If true, Automatically in _id Field creation index . The default is false.3.2 This parameter is no longer supported .
# size( The number ) ( Optional ) Specifies a maximum value for a fixed set , That is, the number of bytes . If capped by true, You also need to specify this field .
# max( The number ) ( Optional ) Specifies the maximum number of documents to contain in a fixed collection .
db.createCollection("wangjing_collection")
### Query existing collections
show collections
show tables
### Delete the collection
db.wangjing_collection.drop()
Four 、 file
### Inserted into the document
# Use insert() or save()
# save() If _id If the primary key exists, update the data , If it doesn't exist, insert data . The method is obsolete in the new version , have access to db.collection.insertOne() or db.collection.replaceOne() Instead of .
# insert() If the inserted data primary key already exists , You'll throw it org.springframework.dao.DuplicateKeyException abnormal , Prompt for duplicate primary key , Do not save current data .
# db.wangjing_collection.save(document)
# 3.2 After the new version db.collection.insertOne() and db.collection.insertMany()
# db.collection.insertOne(<document>,{writeConcern: <document>})
# db.collection.insertMany([ <document 1> , <document 2>, ... ],{writeConcern: <document>, ordered: <boolean>})
# Parameter description :
# document: The document to write .
# writeConcern: Write strategy , The default is 1, That is, it is required to confirm the write operation ,0 Is not required .
# ordered: Specifies whether to write in order , Default true, Write in order .
# If the collection does not exist, it will be created automatically
db.wangjing_collection.insert({"name":"wangjing","sex":" male ","age":27})
### View the inserted document
db.wangjing_collection.find()
### Update the document
# db.collection.update(<query>, <update>, { upsert: <boolean>, multi: <boolean>, writeConcern: <document> })
# Parameter description :
# query : update Query criteria for , similar sql update In the query where hinder .
# update : update Object and some updated operators ( Such as $,$inc...) etc. , It can also be understood as sql update In the query set hinder
# upsert : Optional , This parameter means , If it doesn't exist update The record of , Whether insert objNew,true Insert for , The default is false, Do not insert .
# multi : Optional ,mongodb The default is false, Update only the first record found , If this parameter is zero true, According to the conditions to find out all the records updated .
# writeConcern : Optional , The level at which an exception is thrown .
db.wangjing_collection.update({"name":"wangjing"}, {$set:{"name":"wangjing1"}})
### Delete the document
# db.collection.remove(<query>, { justOne: <boolean>, writeConcern: <document>})
# Parameter description :
# query :( Optional ) Conditions for deleted documents .
# justOne : ( Optional ) If it is set to true or 1, Only one document is deleted , If this parameter is not set , Or use default values false, Deletes all documents that match the criteria .
# writeConcern :( Optional ) The level at which an exception is thrown .
db.wangjing_collection.remove({'name':'wangjing1'})
### Query the document
# db.collection.find(query, projection)
# Parameter description :
# query : Optional , Use the query operator to specify the query criteria
# projection : Optional , Use the projection operator to specify the returned key . Returns all key values in the document when queried , Simply omit the parameter ( The default is omitted ).
# Read data in an easy to read way : pretty()
# db.collection.find(query, projection).pretty()
# Return only one document findOne()
db.wangjing_collection.find({'name':'wangjing'}).pretty()
# MongoDB AND Conditions
db.wangjing_collection.find({"name": "wangjing"},{"sex": " male "}).pretty()
# MongoDB OR Conditions
db.wangjing_collection.find({$or: [{"name": "wangjing"}, {"sex":" male "}]}).pretty()
# AND and OR A combination of
db.wangjing_collection.find({"age": {$gt:20}, $or: [{"name": "wangjing"},{"sex": " male "}]}).pretty()
notes : The above contents are only for reference and exchange , Do not use for commercial purposes , If there is infringement, please contact me to delete !
边栏推荐
- Sum of acwing796 submatrix
- Project training of Shandong University rendering engine system (V)
- Project training of Shandong University rendering engine system (III)
- acwing 高精度乘法
- Batch --03---cmdutil
- acwing 790. The cubic root of a number (floating-point number in half)
- calibration of sth
- acwing 800. Target and of array elements
- canvas 高级功能(下)
- 看《梦华录》上头的人都该尝试下这款抖音特效
猜你喜欢
随机推荐
MongoDB系列之SQL和NoSQL的区别
How to construct PostgreSQL error codes
Interview: difference between '= =' and equals()
Acwing 1927 自动补全(知识点:hash,二分,排序)
<山东大学项目实训>渲染引擎系统(三)
33-【go】Golang sync. Usage of waitgroup - ensure that the go process is completed before the main process exits
并发包和AQS
数据库的三大范式
The C Programming Language(第 2 版) 笔记 / 8 UNIX 系统接口 / 8.5 实例(fopen 和 getc 函数的实现)
leetcode-54. Spiral matrix JS
acwing 高精度乘法
Servlet API
Project training of Shandong University rendering engine system (II)
Acwing794 high precision Division
MySQL interview arrangement
JS monitors whether the user opens the screen focus
Page class of puppeter
【摸鱼神器】UI库秒变LowCode工具——列表篇(一)设计与实现
The C programming language (version 2) notes / 8 UNIX system interface / 8.4 random access (lseek)
<山东大学项目实训>渲染引擎系统(五)









