57-1、2、3 NoSQL基础及MongoDB、MongoDB基本应用、mongodb索引及复制集

前端之家收集整理的这篇文章主要介绍了57-1、2、3 NoSQL基础及MongoDB、MongoDB基本应用、mongodb索引及复制集前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

02MongoDB基本应用


配置环境:

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

修改

dbpath=/var/lib/mongo

dbpath=/mongodb/data

bind_ip=127.0.0.1

#bind_ip=127.0.0.1

#httpinterface=true

httpinterface=true

rest=true

@H_403_174@

[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:631 :::*

LISTEN 0 100 ::1:25 :::*

LISTEN 0 100 127.0.0.1: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

> db.stats()

{

"db" : "testdb",

"collections" : 0,'Microsoft YaHei';font-size:10px;"> "objects" : 0,'Microsoft YaHei';font-size:10px;"> "avgObjSize" : 0,'Microsoft YaHei';font-size:10px;"> "dataSize" : 0,'Microsoft YaHei';font-size:10px;"> "storageSize" : 0,'Microsoft YaHei';font-size:10px;"> "numExtents" : 0,'Microsoft YaHei';font-size:10px;"> "indexes" : 0,'Microsoft YaHei';font-size:10px;"> "indexSize" : 0,'Microsoft YaHei';font-size:10px;"> "fileSize" : 0,'Microsoft YaHei';font-size:10px;"> "dataFileVersion" : {


},'Microsoft YaHei';font-size:10px;"> "ok" : 1

}

> db.version()

2.6.4

> db.getCollectionNames()

[ ]


#插入数据

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

WriteResult({ "nInserted" : 1 })


> show collections

students

system.indexes

admin (empty)

local 0.078GB

testdb 0.078GB


> db.students.stats()

"ns" : "testdb.students",'Microsoft YaHei';font-size:10px;"> "count" : 1,'Microsoft YaHei';font-size:10px;"> "size" : 112,'Microsoft YaHei';font-size:10px;"> "avgObjSize" : 112,'Microsoft YaHei';font-size:10px;"> "storageSize" : 8192,'Microsoft YaHei';font-size:10px;"> "numExtents" : 1,'Microsoft YaHei';font-size:10px;"> "nindexes" : 1,'Microsoft YaHei';font-size:10px;"> "lastExtentSize" : 8192,'Microsoft YaHei';font-size:10px;"> "paddingFactor" : 1,'Microsoft YaHei';font-size:10px;"> "systemFlags" : 1,'Microsoft YaHei';font-size:10px;"> "userFlags" : 1,'Microsoft YaHei';font-size:10px;"> "totalIndexSize" : 8176,'Microsoft YaHei';font-size:10px;"> "indexSizes" : {

"_id_" : 8176

}



[ "students","system.indexes" ]

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

WriteResult({ "nInserted" : 1 })

"ns" : "test.students",'Microsoft YaHei';font-size:10px;">}


#帮助:

> 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()

2

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

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

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

#查询年龄大于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" }


#查询年龄在20到40之间的记录

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

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

#查看年龄不在20到40之间的记录

> db.students.find({Age:{$nin: [20,"age" : 23 }


#查看Age或age值不在20到40之间的记录

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

#查看Age或age值在20到40之间的记录

> db.students.find({$or: [{Age: {$in: [20,{age:{$in:[20,'Microsoft YaHei';font-size:10px;">#查看存在gender字段的记录

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


#查看不存在gender字段的记录

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



#修改"tom"的age为21

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

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

#删除age值为21的记录

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

WriteResult({ "nRemoved" : 1 })

#统计Age值在20到40之间的记录数

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

2


#显示Age值在20到40之间的记录,仅显示满足条件的第一条记录


#显示Age值在20到40之间的记录,第一条记录不显示

#显示Age值大于10的记录,仅显示一条满足条件的记录

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

"_id" : ObjectId("58720b48110b5df571418847"),'Microsoft YaHei';font-size:10px;"> "name" : "Ou Yangfeng",'Microsoft YaHei';font-size:10px;"> "Age" : 90,'Microsoft YaHei';font-size:10px;"> "Course" : "HaMogong"

#删除students表

> db.students.drop()

true

> show collections;

system.indexes


"db" : "test",'Microsoft YaHei';font-size:10px;"> "collections" : 2,'Microsoft YaHei';font-size:10px;"> "objects" : 1,'Microsoft YaHei';font-size:10px;"> "avgObjSize" : 48,'Microsoft YaHei';font-size:10px;"> "dataSize" : 48,'Microsoft YaHei';font-size:10px;"> "storageSize" : 16384,'Microsoft YaHei';font-size:10px;"> "numExtents" : 2,'Microsoft YaHei';font-size:10px;"> "fileSize" : 67108864,'Microsoft YaHei';font-size:10px;"> "nsSizeMB" : 16,'Microsoft YaHei';font-size:10px;"> "dataFileVersion" : {

"major" : 4,'Microsoft YaHei';font-size:10px;"> "minor" : 5

"extentFreeList" : {

"num" : 2,'Microsoft YaHei';font-size:10px;"> "totalSize" : 139264

#删除数据库

> db.dropDatabase()

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


03mongodb索引及复制集[root@node1 ~]# mongo

connecting to: test

> for (i=1;i<=10000;i++) db.students.insert({name:"student"+i,age:(i%120),address:"#85 Wenhua Road,Zhengzhou,China"})

> db.students.find().count()

10000

{ "_id" : ObjectId("5871e94113222f399a5240a3"),'Microsoft YaHei';font-size:10px;">{ "_id" : ObjectId("587232dba7eb9c0c7bd54561"),"name" : "student1","age" : 1,"address" : "#85 Wenhua Road,China" }

{ "_id" : ObjectId("587232dba7eb9c0c7bd54562"),"name" : "student2","age" : 2,'Microsoft YaHei';font-size:10px;">{ "_id" : ObjectId("587232dba7eb9c0c7bd54563"),"name" : "student3","age" : 3,'Microsoft YaHei';font-size:10px;">{ "_id" : ObjectId("587232dba7eb9c0c7bd54564"),"name" : "student4","age" : 4,'Microsoft YaHei';font-size:10px;">{ "_id" : ObjectId("587232dba7eb9c0c7bd54565"),"name" : "student5","age" : 5,'Microsoft YaHei';font-size:10px;">{ "_id" : ObjectId("587232dba7eb9c0c7bd54566"),"name" : "student6","age" : 6,'Microsoft YaHei';font-size:10px;">{ "_id" : ObjectId("587232dba7eb9c0c7bd54567"),"name" : "student7","age" : 7,'Microsoft YaHei';font-size:10px;">{ "_id" : ObjectId("587232dba7eb9c0c7bd54568"),"name" : "student8","age" : 8,'Microsoft YaHei';font-size:10px;">{ "_id" : ObjectId("587232dba7eb9c0c7bd54569"),"name" : "student9","age" : 9,'Microsoft YaHei';font-size:10px;">{ "_id" : ObjectId("587232dba7eb9c0c7bd5456a"),"name" : "student10","age" : 10,'Microsoft YaHei';font-size:10px;">{ "_id" : ObjectId("587232dba7eb9c0c7bd5456b"),"name" : "student11","age" : 11,'Microsoft YaHei';font-size:10px;">{ "_id" : ObjectId("587232dba7eb9c0c7bd5456c"),"name" : "student12","age" : 12,'Microsoft YaHei';font-size:10px;">{ "_id" : ObjectId("587232dba7eb9c0c7bd5456d"),"name" : "student13","age" : 13,'Microsoft YaHei';font-size:10px;">{ "_id" : ObjectId("587232dba7eb9c0c7bd5456e"),"name" : "student14","age" : 14,'Microsoft YaHei';font-size:10px;">{ "_id" : ObjectId("587232dba7eb9c0c7bd5456f"),"name" : "student15","age" : 15,'Microsoft YaHei';font-size:10px;">{ "_id" : ObjectId("587232dba7eb9c0c7bd54570"),"name" : "student16","age" : 16,'Microsoft YaHei';font-size:10px;">{ "_id" : ObjectId("587232dba7eb9c0c7bd54571"),"name" : "student17","age" : 17,'Microsoft YaHei';font-size:10px;">{ "_id" : ObjectId("587232dba7eb9c0c7bd54572"),"name" : "student18","age" : 18,'Microsoft YaHei';font-size:10px;">{ "_id" : ObjectId("587232dba7eb9c0c7bd54573"),"name" : "student19","age" : 19,China" }


#在name字段上构建一个升序索引

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

"createdCollectionAutomatically" : false,'Microsoft YaHei';font-size:10px;"> "numIndexesBefore" : 1,'Microsoft YaHei';font-size:10px;"> "numIndexesAfter" : 2,'Microsoft YaHei';font-size:10px;">#显示表中索引

> db.students.getIndexes()

[

{

"v" : 1,'Microsoft YaHei';font-size:10px;"> "key" : {

"_id" : 1

},'Microsoft YaHei';font-size:10px;"> "name" : "_id_",'Microsoft YaHei';font-size:10px;"> "ns" : "testdb.students"

"name" : 1

"name" : "name_1",'Microsoft YaHei';font-size:10px;"> }

]


#删除索引

> db.students.dropIndex("name_1")

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

#创建唯一索引

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

"unique" : true,'Microsoft YaHei';font-size:10px;">]

#创建唯一索引后,新增相同字段值会提示错误

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

WriteResult({

"nInserted" : 0,'Microsoft YaHei';font-size:10px;"> "writeError" : {

"code" : 11000,'Microsoft YaHei';font-size:10px;"> "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,'Microsoft YaHei';font-size:10px;">#显示查找语句执行过程

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

"cursor" : "BtreeCursor name_1",'Microsoft YaHei';font-size:10px;"> "isMultiKey" : false,'Microsoft YaHei';font-size:10px;"> "n" : 1,'Microsoft YaHei';font-size:10px;"> "nscannedObjects" : 1,'Microsoft YaHei';font-size:10px;"> "nscanned" : 1,'Microsoft YaHei';font-size:10px;"> "nscannedObjectsAllPlans" : 1,'Microsoft YaHei';font-size:10px;"> "nscannedAllPlans" : 1,'Microsoft YaHei';font-size:10px;"> "scanAndOrder" : false,'Microsoft YaHei';font-size:10px;"> "indexOnly" : false,'Microsoft YaHei';font-size:10px;"> "nYields" : 0,'Microsoft YaHei';font-size:10px;"> "nChunkSkips" : 0,'Microsoft YaHei';font-size:10px;"> "millis" : 0,'Microsoft YaHei';font-size:10px;"> "indexBounds" : {

"name" : [

[

"student5000",'Microsoft YaHei';font-size:10px;"> "student5000"

]

]

"server" : "node1:27017",'Microsoft YaHei';font-size:10px;"> "filterSet" : false

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

"n" : 5552,'Microsoft YaHei';font-size:10px;"> "nscannedObjects" : 5552,'Microsoft YaHei';font-size:10px;"> "nscanned" : 5552,'Microsoft YaHei';font-size:10px;"> "nscannedObjectsAllPlans" : 5552,'Microsoft YaHei';font-size:10px;"> "nscannedAllPlans" : 5552,'Microsoft YaHei';font-size:10px;"> "nYields" : 43,'Microsoft YaHei';font-size:10px;"> "millis" : 13,'Microsoft YaHei';font-size:10px;"> {


}

}

原文链接:https://www.f2er.com/nosql/203550.html

猜你在找的NoSQL相关文章