Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Mongodb uses 2

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

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 similar to mysql

[root@localhost] # mongo-- port 27017

MongoDB shell version: 3.0.7

Connecting to: 127.0.0.1:27017/test

>

[root@localhost ~] # mongo / / equivalent to mongo-- port 27017 similar to direct mysql direct connection

MongoDB shell version: 3.0.7

Connecting to: test

>

[root@localhost ~] # mongo-- host 127.0.0.1 # specify host name and ip

MongoDB shell version: 3.0.7

Connecting to: 127.0.0.1:27017/test

>

[root@localhost ~] # netstat-lnp | grep mongo

Tcp 0 0 0.0.0 0 27017 0.0.0 0 15 * LISTEN 3769/mongod

Unix 2 [ACC] STREAM LISTENING 21107 3769/mongod / tmp/mongodb-27017.sock

[root@localhost ~] # ps aux | grep mongo

Mongod 3769 0.5 2.4 500844 47660? Sl 10:57 0:17 / usr/bin/mongod-f / etc/mongod.conf

Root 7198 0.0 103308 860 pts/1 S+ 11:54 0:00 grep mongo

2) user management

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

# create a user

First of all, mongodb users are for the library. When you set up a user, you need to enter the corresponding library first.

> use test # Select a library. If the library does not exist before, it will be created automatically

Switched to db test

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

Successfully added user: {

"user": "admin"

"roles": [

{

"role": "dbOwner"

"db": "userdb"

}

]

}

# View users

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

> use admin # all users are under this library

Switched to db admin

> db.system.users.find ()

{"_ id": "test.admin", "user": "admin", "db": "test", "credentials": {"SCRAM-SHA-1": {"iterationCount": 10000, "salt": "GXrf4fS8qPBncxKo2ElryA==", "storedKey": "C7DFv8iY0gxXgFCgQC QCpicjbCGWCMU =", "serverKey": "FfbqE2RglKh55DOxy4VmN8ptSEA="}, "roles": [{"role": "dbOwner" "db": "userdb"}]}

>

Show users / / View all users under the current library

# to delete a user, you need to switch to the admin library

Db.dropUser ('admin')

> use test # switch to the previously established library to delete previously created users

Switched to db test

> db.dropUser ('admin')

True

> show users

> db.system.users.find () # and found that there are no users

3) Database management

# View version

MongoDB shell version: 3.0.7

Connecting to: test

> db.version ()

3.0.7

# switch / create a library

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

Switched to db userdb

# 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.

> use userdb # you must create a collection when you create a library but do not have a show at this time

Switched to db userdb

> show dbs

Admin 0.078GB

Local 0.078GB

> db.createCollection ('clo1') # create a collection whose name is clo1

{"ok": 1}

> show dbs

Admin 0.078GB

Local 0.078GB

Userdb 0.078GB # will now display the library

>

# View all databases

Show dbs

# Delete database mydb

> show dbs

Admin 0.078GB

Local 0.078GB

Userdb 0.078GB

> use userdb # switch to the library first

Switched to db userdb

> db.dropDatabase ()

{"dropped": "userdb", "ok": 1}

> show dbs

Admin 0.078GB

Local 0.078GB # is gone at this time. I deleted the userdb library before.

# View current library information

> use admin / / switch to the library first

Switched to db admin

> db.stats ()

{

"db": "admin"

"collections": 4

"objects": 10

"avgObjSize": 67.2

"dataSize": 672

"storageSize": 28672

"numExtents": 4

"indexes": 3

"indexSize": 24528

"fileSize": 67108864

"nsSizeMB": 16

"extentFreeList": {

"num": 0

"totalSize": 0

}

"dataFileVersion": {

"major": 4

"minor": 22

}

"ok": 1

}

>

> db.serverStatus ()

{

"host": "localhost.localdomain"

"version": "3.0.7"

"process": "mongod"

"pid": NumberLong (3769)

"uptime": 15510

"uptimeMillis": NumberLong (15509761)

"uptimeEstimate": 14138

LocalTime: ISODate ("2015-12-14T07:16:10.249Z")

"asserts": {

"regular": 0

"warning": 0

"msg": 0

"user": 0

"rollovers": 0

}

"backgroundFlushing": {

Flushes: 258

"total_ms": 25

"average_ms": 0.0989922480620156

"last_ms": 0

Last_finished: ISODate ("2015-12-14T07:15:40.721Z")

}

.

4) data management

Create a collection

> use mydb

Switched to db mydb

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

{"ok": 1}

# mycol is the name of the collection

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.

> db.createCollection ('caimz')

{"ok": 1}

> show collections

Caimz

Mycol

System.indexes

View the collection

# show collections

> show collections

Caimz

Mycol

System.indexes

> show tables

Caimz

Mycol

System.indexes

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.insert ({AccountID:2,UserName: "123", password:" 123456 "})

WriteResult ({"nInserted": 1})

> show tables

Account

Caimz

Mycol

System.indexes

>

Modify

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

View

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

> db.Account.insert ({AccountID:2,UserName: "aaa", password: "123456"})

WriteResult ({"nInserted": 1})

> db.Account.find ()

{"_ id": ObjectId ("566e6f7f269ae27bd4c1929f"), "AccountID": 2, "UserName": "aaa", "password": "123456"}

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

> db.Account.find ()

{"_ id": ObjectId ("566e6f1f269ae27bd4c1929e"), "AccountID": 2, "UserName": "123", "password": "123456"}

{"_ id": ObjectId ("566e6f7f269ae27bd4c1929f"), "AccountID": 2, "UserName": "aaa", "password": "123456"}

{"_ id": ObjectId ("566e6fab269ae27bd4c192a0"), "AccountID": 1, "UserName": "aaa", "password": "123456"}

> db.Account.find ({AccountID:1})

{"_ id": ObjectId ("566e6fab269ae27bd4c192a0"), "AccountID": 1, "UserName": "aaa", "password": "123456"}

> db.caimz.find ({UserName: "2"})

Update:

> db.Account.update ({AccountID:1}, {"$set": {"Age": 20}})

WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1})

> db.Account.find ()

{"_ id": ObjectId ("566e6f1f269ae27bd4c1929e"), "AccountID": 2, "UserName": "123", "password": "123456"}

{"_ id": ObjectId ("566e6f7f269ae27bd4c1929f"), "AccountID": 2, "UserName": "aaa", "password": "123456"}

{"_ id": ObjectId ("566e6fab269ae27bd4c192a0"), "AccountID": 1, "UserName": "aaa", "password": "123456", "Age": 20}

> db.caimz.update ({AccountID:1}, {"$set": {"Age": 19}})

WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1})

> db.caimz.find ()

{"_ id": ObjectId ("566e7042c796070f15a8a72d"), "AccountID": 1, "UserName": "1", "password": "123456", "Age": 19}

{"_ id": ObjectId ("566e7049c796070f15a8a72e"), "AccountID": 2, "UserName": "2", "password": "123456"}

> db.caimz.update ({AccountID:1}, {"$set": {age:19}})

WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1})

> db.caimz.find ()

{"_ id": ObjectId ("566e7042c796070f15a8a72d"), "AccountID": 1, "UserName": "1", "password": "123456", "Age": 19, "age": 19}

{"_ id": ObjectId ("566e7049c796070f15a8a72e"), "AccountID": 2, "UserName": "2", "password": "123456"}

>

Delete

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

> db.caimz.find ()

{"_ id": ObjectId ("566e7042c796070f15a8a72d"), "AccountID": 1, "UserName": "1", "password": "123456", "Age": 1, "age": 19}

{"_ id": ObjectId ("566e7049c796070f15a8a72e"), "AccountID": 2, "UserName": "2", "password": "123456"}

> db.caimz.remove ({age:19})

WriteResult ({"nRemoved": 1})

> db.caimz.find ()

{"_ id": ObjectId ("566e7049c796070f15a8a72e"), "AccountID": 2, "UserName": "2", "password": "123456"}

>

To delete the entire document

Db.Account.drop ()

> show tables

Account

Caimz

Mycol

System.indexes

> db.Account.drop ()

True

> show tables

Caimz

Mycol

System.indexes

>

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 () / / check the status of the mongodb server and list all the collection in the library

> db.printCollectionStats ()

Caimz

{

"ns": "mydb.caimz"

"count": 1

"size": 112

"avgObjSize": 112

"numExtents": 1

"storageSize": 8192

"lastExtentSize": 8192

"paddingFactor": 1

"paddingFactorNote": "paddingFactor is unused and unmaintained in 3.0. It remains hard coded to 1.0 for compatibility only."

"userFlags": 1

.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report