Mongodb Daily Use Commands

Keywords: Database MongoDB Session

1, connection

# mongo 192.168.0.214:27017
# mongodb://Admin: 123456@localhost/test admin User logs in to the test library with password 123456
# mongodb://admin:123456@localhost:27017,localhost:27018,localhost:27019
# mongodb://admin:123456@host1,host2,host3/?slaveOk=true # connect to three replica sets of servers, write operations in the main library, queries distributed to the slave Library
# mongodb://host1,host2,host3/?safe=true;w=2;wtimeoutMS=2000 # connect to replica set in secure mode and wait for at least two replica servers to write successfully, timeout set to 2 seconds

2. Create users and modify permissions

>db.createUser({user: "test",pwd: "123456",roles: [ { role: "dbOwner", db: "coinsuper_whitebox" }]})      #The most commonly used one is that dbowner can do whatever it wants in its own library
>db.createUser({user:"usertest",pwd:"passtest",roles:[  {role:"clusterAdmin", db:"admin" }, {role:"readAnyDatabase",db:"admin" }, {role:"readWrite",db:"testDB" } ]})
>db.updateUser("usertest",{roles:[ {role:"read",db:"coinsuper_whitebox"} ]})                     #Modified to read-only permissions

> use coinsuper_whitebox
>db.addUser("test2","123456")                            #Configuring Read-Write Users
>db.addUser("test2","123456",true)                #Configuring Read-Only Users

3. Create a collection:

> use coinsuper_whitebox
>show collections
>db.auth("test","123456")
> db.tt.insert ({id:1, name: "test"}) # This automatically creates collections
 > db. createCollection ("Account") # can also create collections like this
>db.createCollection("Test",{capped:true, size:10000}) { "ok" : 1 }
Size: Specify a certain space size at the beginning of the table. The next insertion operation will continue in the order of APPEND data in this pre-allocated space file. If the space size has exceeded, then return to the file header to cover the original data and continue inserting. This structure guarantees the efficiency of insertion and query. It does not allow deletion of single records, and updates are limited: it cannot exceed the size of the original records. This table is very efficient. It is suitable for temporary data storage occasions, such as session information of logged-in users in websites, and monitoring logs of some programs, which belong to data that can be covered after a certain period of time.
capped:true, meaning that the structure of the set cannot be modified;

4. Modify the table name

> db.Account.renameCollection("Account1")
>db.Account.help()                       #view help

5, query

> db.Account.findOne()                #Look up a record
> DBQuery.shellBatchSize = 100               #Set each page to display 100 records, default is 20, input it can display the next page.
> db.Account.distinct("UserName")                                          #Different records of fields in query aggregation
> db.Account.find({"UserName":/^keyword/})                        #UserName records in query aggregation that begin with "keyword"
> db.Account.find({},{"UserName":1,"Email":1})                #Query username and email columns
> db.Account.find({},{"UserName":0})                                 #Exclusion of specified columns in query aggregation
> db.Account.find({"Age":{"$gt":20}},{"UserName":1})        #select username where age>20
> db.Account.find().sort({"UserName":1})                             #username:1 denotes ascending order and username:-1 denotes ordering
> db.Account.find().count()
> db.Account.find({"Age":{"$gt":20}}).count()                    #How many records have statistical age greater than 20
> db.Account.find({"UserName":{"$exists":true}}).count()         #select count(UserName)
> db.Account.find().skip(10).limit(5)                                                    #Five records after Article 10 in the query set

6, modify

> db.Account.insert({AccountID:2,UserName:"lb",Password:"1",Age:25,Email:"libing@163.com",RegisterDate:"2011-06-09 16:36:95"})           #Add new records
> db.Account.update({"AccountID":1},{"$set":{"Age":27,"Email":"libingql@163.com"}})                 #Modification record
> db.Account.update({"AccountID":1},{"$inc":{"Age":1}})           
> db.Account.remove({"UserName":"libing"})
> db.Account.remove({"Age":{$lt:20}})
> db.Account.remove()                      #Delete all

7, delete

>db.dropuser('test')     
>use coinsuper_whitebox
>db.dropDatabase()

Posted by alvinho on Fri, 12 Apr 2019 02:06:31 -0700