Get the App
SLTechnology News&Howtos  ›  Database  › 

Mongodb usage

Shulou Source: shulou.com Published: 2022-06-01 12:13:21 09月12日 Update

Mongodb usage

1) Connect to mongodb

Mongo-- port 10001 / / specify port. The default port is 27017, so the local mongodb is connected.

Mongo-- host 192.168.0.11 / / Connect to a remote mongodb

Mongo-umyuser-p123456 / / Log in with username and password

2) user management

User role: http://bbs.51cto.com/thread-1146654-1.html

# create a user

Use test / / switch library. If there is no such library, it will be created automatically.

Db.createUser ({user: "admin", pwd: "123456", roles: [{role:'dbOwner',db:'userdb'}]})

# View users

Db.system.users.find () / / lists all users. You need to switch to the admin library (use admin)

Show users / / View all users under the current library

Db.dropUser ('admin') / / to delete a user, you need to switch to the admin library

To log in with your account and password, you need to open the user authentication: http://theadorelee.com/index.php/archives/60.html

3) Database management

# View version

Db.version ()

# display the current library db

# switch / create a library

Use userdb / / switch if the library exists, and create it if it does not exist

# show dbs cannot see userdb at this time, so we need to create a collection

Db.createCollection ('clo1')

If you show dbs again, you will have userdb.

# View all databases

Show dbs

# Delete database mydb

Use mydb / / switch to the library before deleting it

Db.dropDatabase ()

Db.stats () / / View the information of the current library

Db.serverStatus () / / View the status of the mongodb server

4) data management

Create a collection

Db.createCollection ("mycol", {capped: true, autoIndexID: true, size: 6142800, max: 10000})

Syntax: db.createCollection (name,options)

Name is the name of the collection. Options is optional and is used to configure the parameters of the collection. Parameters are as follows

Capped true/false (optional) enables capped collections if true. A capped collection is a fixed-size collection that automatically overwrites the oldest entry when it reaches its maximum size. If you specify true, you also need to specify size parameters.

AutoindexID true/false (optional) if true, the default value for the automatically created index _ id field is false.

Size (optional) specifies the maximum size byte capped collection. If the cap is true, then you also need to specify this field. Unit B

Max (optional) specifies the maximum number of files allowed in the capped collection.

Show collections / / View the collection

Add a document to the collection

Db.Account.insert ({AccountID:2,UserName: "123", password:" 123456 "}) / / if the collection does not exist and the data is inserted directly, mongodb will automatically create the collection

Db.Account.update ({AccountID:2}, {"$set": {"Age": 20}}) / / Update

Db.Account.find () / / View all documents

Db.Account.find ({AccountID:2}) / / query deletion based on criteria

Db.Account.remove ({AccountID:2}) / / delete according to condition

Db.Account.drop () / / to delete the entire document

View the status of the collection

Use dbname / / enter the corresponding library first, and then check the collection status

Db.printCollectionStats ()

5) Database performance

Db.stats () / / View the information of the current library

Db.serverStatus () / / View the status of the mongodb server

Tags: User data switching capping database status maximum parameter size document management information field password server condition service unit name byte Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Redmi Microsoft macOS MariaDB