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

57-1, 2, 3 NoSQL Foundation and MongoDB, Mongo

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

Basic application of 02MongoDB

Configure the environment:

Node1:192.168.1.121CentOS release 6.7

[root@node1 ~] # cd mongodb-2.6.4/

[root@node1 mongodb-2.6.4] # ls

Mongodb-org-2.6.4-1.x86_64.rpm mongodb-org-shell-2.6.4-1.x86_64.rpm

Mongodb-org-mongos-2.6.4-1.x86_64.rpm mongodb-org-tools-2.6.4-1.x86_64.rpm

Mongodb-org-server-2.6.4-1.x86_64.rpm

[root@node1 mongodb-2.6.4] # yum install mongodb-org-server-2.6.4-1.x86_64.rpm mongodb-org-shell-2.6.4-1.x86_64.rpm mongodb-org-tools-2.6.4-1.x86_64.rpm-y

[root@node1 mongodb-2.6.4] # cd

[root@node1] # mkdir-p / mongodb/data

[root@node1 ~] # id mongod

Uid=496 (mongod) gid=492 (mongod) groups=492 (mongod)

[root@node1] # chown-R mongod.mongod / mongodb/

[root@node1 ~] # vim / etc/mongod.conf

Modify

Dbpath=/var/lib/mongo

For

Dbpath=/mongodb/data

Modify

Bind_ip=127.0.0.1

For

# bind_ip=127.0.0.1

Modify

# httpinterface=true

For

Httpinterface=true

Rest=true

[root@node1 ~] # service mongod start

[root@node1 ~] # ls / mongodb/data/

Journal local.0 local.ns mongod.lock _ tmp

[root@node1 ~] # du-sh / mongodb/data/

3.1G / mongodb/data/

[root@node1 ~] # ss-tnl

State Recv-Q Send-Q Local Address:Port Peer Address:Port

LISTEN 0 128:: 39591: *

LISTEN 0 128 *: 27017 *: *

LISTEN 0 128: 111: *

LISTEN 0 128 *: 111 *: *

LISTEN 0 128 *: 28017 *: *

LISTEN 0 128 *: 46453 *: *

LISTEN 0 128:: 22: *

LISTEN 0 128 *: 22 *: *

LISTEN 0 128 127.0.0.1 631 *: *

LISTEN 0 128:: 1 LISTEN:: *

LISTEN 0 100:: 1:25: *

LISTEN 0 100 127.0.0.1 purl 25 *: *

[root@node1] # mongo-- host 192.168.1.121

MongoDB shell version: 2.6.4

Connecting to: 192.168.1.121:27017/test

Welcome to the MongoDB shell.

For interactive help, type "help".

For more comprehensive documentation, see

Http://docs.mongodb.org/

Questions? Try the support group

Http://groups.google.com/group/mongodb-user

> show dbs

Admin (empty)

Local 0.078GB

> use testdb

Switched to db testdb

> show dbs

Admin (empty)

Local 0.078GB

> db.stats ()

{

"db": "testdb"

"collections": 0

"objects": 0

"avgObjSize": 0

"dataSize": 0

"storageSize": 0

"numExtents": 0

"indexes": 0

"indexSize": 0

"fileSize": 0

"dataFileVersion": {

}

"ok": 1

}

> db.version ()

2.6.4

> db.getCollectionNames ()

[]

# insert data

> db.students.insert ({name: "tom", age:23})

WriteResult ({"nInserted": 1})

> show collections

Students

System.indexes

> show dbs

Admin (empty)

Local 0.078GB

Testdb 0.078GB

> db.students.stats ()

{

"ns": "testdb.students"

"count": 1

"size": 112

"avgObjSize": 112

"storageSize": 8192

"numExtents": 1

"nindexes": 1

"lastExtentSize": 8192

"paddingFactor": 1

"systemFlags": 1

"userFlags": 1

"totalIndexSize": 8176

"indexSizes": {

"_ id_": 8176

}

"ok": 1

}

> db.getCollectionNames ()

["students", "system.indexes"]

> db.students.insert ({name: "jerry", age:40,gender: "M"})

WriteResult ({"nInserted": 1})

> db.students.stats ()

{

"ns": "test.students"

"count": 1

"size": 112

"avgObjSize": 112

"storageSize": 8192

"numExtents": 1

"nindexes": 1

"lastExtentSize": 8192

"paddingFactor": 1

"systemFlags": 1

"userFlags": 1

"totalIndexSize": 8176

"indexSizes": {

"_ id_": 8176

}

"ok": 1

}

# help:

> db.mycoll.help ()

> db.mycoll.find.help ()

> db.students.find ()

{"_ id": ObjectId ("58720933110b5df571418845"), "name": "jerry", "age": 40, "gender": "M"}

{"_ id": ObjectId ("58720a26110b5df571418846"), "name": "tom", "age": 23}

> db.students.count ()

two

> db.students.insert ({name: "Ou Yangfeng", Age:90,Course: "HaMogong"})

WriteResult ({"nInserted": 1})

> db.students.insert ({name: "Yang Guo", Age:20,Course: "Meinv Quan"})

WriteResult ({"nInserted": 1})

> db.students.insert ({name: "Guo Jing", Age:40,Course: "Xiangchong Shibazhang"})

WriteResult ({"nInserted": 1})

# query records older than 30

> db.students.find ({Age: {$gt: 30}})

{"_ id": ObjectId ("58720b48110b5df571418847"), "name": "Ou Yangfeng", "Age": 90, "Course": "HaMogong"}

{"_ id": ObjectId ("58720ba8110b5df571418849"), "name": "Guo Jing", "Age": 40, "Course": "Xiangchong Shibazhang"}

# query records between 20 and 40 years old

> db.students.find ({Age: {$in: [20jue 40]}})

{"_ id": ObjectId ("58720b71110b5df571418848"), "name": "Yang Guo", "Age": 20, "Course": "Meinv Quan"}

{"_ id": ObjectId ("58720ba8110b5df571418849"), "name": "Guo Jing", "Age": 40, "Course": "Xiangchong Shibazhang"}

# check records that are not between 20 and 40

> db.students.find ({Age: {$nin: [20jue 40]}})

{"_ id": ObjectId ("58720933110b5df571418845"), "name": "jerry", "age": 40, "gender": "M"}

{"_ id": ObjectId ("58720a26110b5df571418846"), "name": "tom", "age": 23}

{"_ id": ObjectId ("58720b48110b5df571418847"), "name": "Ou Yangfeng", "Age": 90, "Course": "HaMogong"}

# View records whose Age or age values are not between 20 and 40

> db.students.find ({$or: [{Age: {$nin: [20jue 40]}}, {age: {$nin: [20jue 40]}}]})

{"_ id": ObjectId ("58720933110b5df571418845"), "name": "jerry", "age": 40, "gender": "M"}

{"_ id": ObjectId ("58720a26110b5df571418846"), "name": "tom", "age": 23}

{"_ id": ObjectId ("58720b48110b5df571418847"), "name": "Ou Yangfeng", "Age": 90, "Course": "HaMogong"}

{"_ id": ObjectId ("58720b71110b5df571418848"), "name": "Yang Guo", "Age": 20, "Course": "Meinv Quan"}

{"_ id": ObjectId ("58720ba8110b5df571418849"), "name": "Guo Jing", "Age": 40, "Course": "Xiangchong Shibazhang"}

# View records with Age or age values between 20 and 40

> db.students.find ({$or: [{Age: {$in: [20jue 40]}}, {age: {$in: [20jue 40]}}]})

{"_ id": ObjectId ("58720933110b5df571418845"), "name": "jerry", "age": 40, "gender": "M"}

{"_ id": ObjectId ("58720b71110b5df571418848"), "name": "Yang Guo", "Age": 20, "Course": "Meinv Quan"}

{"_ id": ObjectId ("58720ba8110b5df571418849"), "name": "Guo Jing", "Age": 40, "Course": "Xiangchong Shibazhang"}

# View records with gender field

> db.students.find ({gender: {$exists: true}})

{"_ id": ObjectId ("58720933110b5df571418845"), "name": "jerry", "age": 40, "gender": "M"}

# View records for which there is no gender field

> db.students.find ({gender: {$exists: false}})

{"_ id": ObjectId ("58720a26110b5df571418846"), "name": "tom", "age": 23}

{"_ id": ObjectId ("58720b48110b5df571418847"), "name": "Ou Yangfeng", "Age": 90, "Course": "HaMogong"}

{"_ id": ObjectId ("58720b71110b5df571418848"), "name": "Yang Guo", "Age": 20, "Course": "Meinv Quan"}

{"_ id": ObjectId ("58720ba8110b5df571418849"), "name": "Guo Jing", "Age": 40, "Course": "Xiangchong Shibazhang"}

# modify the age of "tom" to 21

> db.students.find ()

{"_ id": ObjectId ("58720933110b5df571418845"), "name": "jerry", "age": 40, "gender": "M"}

{"_ id": ObjectId ("58720a26110b5df571418846"), "name": "tom", "age": 23}

{"_ id": ObjectId ("58720b48110b5df571418847"), "name": "Ou Yangfeng", "Age": 90, "Course": "HaMogong"}

{"_ id": ObjectId ("58720b71110b5df571418848"), "name": "Yang Guo", "Age": 20, "Course": "Meinv Quan"}

{"_ id": ObjectId ("58720ba8110b5df571418849"), "name": "Guo Jing", "Age": 40, "Course": "Xiangchong Shibazhang"}

> db.students.update ({name: "tom"}, {$set: {age: 21}})

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

> db.students.find ()

{"_ id": ObjectId ("58720933110b5df571418845"), "name": "jerry", "age": 40, "gender": "M"}

{"_ id": ObjectId ("58720a26110b5df571418846"), "name": "tom", "age": 21}

{"_ id": ObjectId ("58720b48110b5df571418847"), "name": "Ou Yangfeng", "Age": 90, "Course": "HaMogong"}

{"_ id": ObjectId ("58720b71110b5df571418848"), "name": "Yang Guo", "Age": 20, "Course": "Meinv Quan"}

{"_ id": ObjectId ("58720ba8110b5df571418849"), "name": "Guo Jing", "Age": 40, "Course": "Xiangchong Shibazhang"}

# Delete records with an age value of 21

> db.students.remove ({age:21})

WriteResult ({"nRemoved": 1})

> db.students.find ()

{"_ id": ObjectId ("58720933110b5df571418845"), "name": "jerry", "age": 40, "gender": "M"}

{"_ id": ObjectId ("58720b48110b5df571418847"), "name": "Ou Yangfeng", "Age": 90, "Course": "HaMogong"}

{"_ id": ObjectId ("58720b71110b5df571418848"), "name": "Yang Guo", "Age": 20, "Course": "Meinv Quan"}

{"_ id": ObjectId ("58720ba8110b5df571418849"), "name": "Guo Jing", "Age": 40, "Course": "Xiangchong Shibazhang"}

# count the number of records with an Age value between 20 and 40

> db.students.find ({Age: {$in: [20jue 40]}}) .count ()

two

# display records with Age values between 20 and 40, and only the first record that meets the criteria

> db.students.find ({Age: {$in: [20JE40]}}) .limit (1)

{"_ id": ObjectId ("58720b71110b5df571418848"), "name": "Yang Guo", "Age": 20, "Course": "Meinv Quan"}

# displays records with Age values between 20 and 40, but does not show the first record

> db.students.find ({Age: {$in: [20JE40]}}) .skip (1)

{"_ id": ObjectId ("58720ba8110b5df571418849"), "name": "Guo Jing", "Age": 40, "Course": "Xiangchong Shibazhang"}

# display records with Age values greater than 10, and only one record that meets the criteria

> db.students.findOne ({Age: {$gt: 10}})

{

"_ id": ObjectId ("58720b48110b5df571418847")

"name": "Ou Yangfeng"

"Age": 90

"Course": "HaMogong"

}

# Delete students table

> db.students.drop ()

True

> show collections

System.indexes

> db.stats ()

{

"db": "test"

"collections": 2

"objects": 1

"avgObjSize": 48

"dataSize": 48

"storageSize": 16384

"numExtents": 2

"indexes": 0

"indexSize": 0

"fileSize": 67108864

"nsSizeMB": 16

"dataFileVersion": {

"major": 4

"minor": 5

}

"extentFreeList": {

"num": 2

"totalSize": 139264

}

"ok": 1

}

# Delete database

> db.dropDatabase ()

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

> show dbs

Admin (empty)

Local 0.078GB

Testdb 0.078GB

03mongodb Index and replication set

[root@node1 ~] # mongo

MongoDB shell version: 2.6.4

Connecting to: test

> use testdb

Switched to db testdb

> for (iSuppli db.students.find (). Count ()

10000

> db.students.find ()

{"_ id": ObjectId ("5871e94113222f399a5240a3"), "name": "tom", "age": 23}

{"_ id": ObjectId ("587232dba7eb9c0c7bd54561"), "name": "student1", "age": 1, "address": "# 85 Wenhua Road, Zhengzhou, China"}

{"_ id": ObjectId ("587232dba7eb9c0c7bd54562"), "name": "student2", "age": 2, "address": "# 85 Wenhua Road, Zhengzhou, China"}

{"_ id": ObjectId ("587232dba7eb9c0c7bd54563"), "name": "student3", "age": 3, "address": "# 85 Wenhua Road, Zhengzhou, China"}

{"_ id": ObjectId ("587232dba7eb9c0c7bd54564"), "name": "student4", "age": 4, "address": "# 85 Wenhua Road, Zhengzhou, China"}

{"_ id": ObjectId ("587232dba7eb9c0c7bd54565"), "name": "student5", "age": 5, "address": "# 85 Wenhua Road, Zhengzhou, China"}

{"_ id": ObjectId ("587232dba7eb9c0c7bd54566"), "name": "student6", "age": 6, "address": "# 85 Wenhua Road, Zhengzhou, China"}

{"_ id": ObjectId ("587232dba7eb9c0c7bd54567"), "name": "student7", "age": 7, "address": "# 85 Wenhua Road, Zhengzhou, China"}

{"_ id": ObjectId ("587232dba7eb9c0c7bd54568"), "name": "student8", "age": 8, "address": "# 85 Wenhua Road, Zhengzhou, China"}

{"_ id": ObjectId ("587232dba7eb9c0c7bd54569"), "name": "student9", "age": 9, "address": "# 85 Wenhua Road, Zhengzhou, China"}

{"_ id": ObjectId ("587232dba7eb9c0c7bd5456a"), "name": "student10", "age": 10, "address": "# 85 Wenhua Road, Zhengzhou, China"}

{"_ id": ObjectId ("587232dba7eb9c0c7bd5456b"), "name": "student11", "age": 11, "address": "# 85 Wenhua Road, Zhengzhou, China"}

{"_ id": ObjectId ("587232dba7eb9c0c7bd5456c"), "name": "student12", "age": 12, "address": "# 85 Wenhua Road, Zhengzhou, China"}

{"_ id": ObjectId ("587232dba7eb9c0c7bd5456d"), "name": "student13", "age": 13, "address": "# 85 Wenhua Road, Zhengzhou, China"}

{"_ id": ObjectId ("587232dba7eb9c0c7bd5456e"), "name": "student14", "age": 14, "address": "# 85 Wenhua Road, Zhengzhou, China"}

{"_ id": ObjectId ("587232dba7eb9c0c7bd5456f"), "name": "student15", "age": 15, "address": "# 85 Wenhua Road, Zhengzhou, China"}

{"_ id": ObjectId ("587232dba7eb9c0c7bd54570"), "name": "student16", "age": 16, "address": "# 85 Wenhua Road, Zhengzhou, China"}

{"_ id": ObjectId ("587232dba7eb9c0c7bd54571"), "name": "student17", "age": 17, "address": "# 85 Wenhua Road, Zhengzhou, China"}

{"_ id": ObjectId ("587232dba7eb9c0c7bd54572"), "name": "student18", "age": 18, "address": "# 85 Wenhua Road, Zhengzhou, China"}

{"_ id": ObjectId ("587232dba7eb9c0c7bd54573"), "name": "student19", "age": 19, "address": "# 85 Wenhua Road, Zhengzhou, China"}

# build an ascending index on the name field

> db.students.ensureIndex ({name: 1})

{

"createdCollectionAutomatically": false

"numIndexesBefore": 1

"numIndexesAfter": 2

"ok": 1

}

# display the index in the table

> db.students.getIndexes ()

[

{

"v": 1

"key": {

"_ id": 1

}

"name": "_ id_"

"ns": "testdb.students"

}

{

"v": 1

"key": {

"name": 1

}

"name": "name_1"

"ns": "testdb.students"

}

]

# Delete index

> db.students.dropIndex ("name_1")

{"nIndexesWas": 2, "ok": 1}

> db.students.getIndexes ()

[

{

"v": 1

"key": {

"_ id": 1

}

"name": "_ id_"

"ns": "testdb.students"

}

]

# create a unique index

> db.students.ensureIndex ({name: 1}, {unique: true})

{

"createdCollectionAutomatically": false

"numIndexesBefore": 1

"numIndexesAfter": 2

"ok": 1

}

> db.students.getIndexes ()

[

{

"v": 1

"key": {

"_ id": 1

}

"name": "_ id_"

"ns": "testdb.students"

}

{

"v": 1

"unique": true

"key": {

"name": 1

}

"name": "name_1"

"ns": "testdb.students"

}

]

# after creating a unique index, adding the same field value will prompt an error

> db.students.insert ({name: "student20", age: 20,})

WriteResult ({

"nInserted": 0

"writeError": {

"code": 11000

"errmsg": "insertDocument:: caused by:: 11000 E11000 duplicate key error index: testdb.students.$name_1 dup key: {:\" student20\ "}"

}

})

> db.students.find ({name: "student5000"})

{"_ id": ObjectId ("587232e4a7eb9c0c7bd558e8"), "name": "student5000", "age": 80, "address": "# 85 Wenhua Road, Zhengzhou, China"}

# display the execution process of the lookup statement

> db.students.find ({name: "student5000"}) .explain ()

{

"cursor": "BtreeCursor name_1"

"isMultiKey": false

"n": 1

"nscannedObjects": 1

"nscanned": 1

"nscannedObjectsAllPlans": 1

"nscannedAllPlans": 1

"scanAndOrder": false

"indexOnly": false

"nYields": 0

"nChunkSkips": 0

"millis": 0

"indexBounds": {

"name": [

[

"student5000"

"student5000"

]

]

}

"server": "node1:27017"

"filterSet": false

}

> db.students.find ({name: {$gt: "student5000"}}) .explain ()

{

"cursor": "BtreeCursor name_1"

"isMultiKey": false

"n": 5552

"nscannedObjects": 5552

"nscanned": 5552

"nscannedObjectsAllPlans": 5552

"nscannedAllPlans": 5552

"scanAndOrder": false

"indexOnly": false

"nYields": 43

"nChunkSkips": 0

"millis": 13

"indexBounds": {

"name": [

[

"student5000"

{

}

]

]

}

"server": "node1:27017"

"filterSet": false

}

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