mongodb数据库安装及常见操作

客户端和服务端的安装
# rpm -ivh mongo-10gen-2.4.6-mongodb_1.x86_64.rpm mongo-10gen-server-2.4.6-mongodb_1.x86_64.rpm


创建数据库存放数据目录
# mkdir -pv /mongodb/data
mkdir: created directory `/mongodb'
mkdir: created directory `/mongodb/data'
[root@node2 ~]# chown -R mongod.mongod /mongodb/data


修改配置文件

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


[root@node2 ~]# service mongod start


可以通过访问web界面显示服务器状态:
http://192.168.8.39:28017/


数据库常见操作:
进入数据库
# mongo


> help
	db.help()                    help on db methods
	db.mycoll.help()             help on collection methods
	sh.help()                    sharding helpers
	rs.help()                    replica set helpers
	help admin                   administrative help
	help connect                 connecting to a db help
	help keys                    key shortcuts
	help misc                    misc things to know
	help mr                      mapreduce


	show dbs                     show database names
	show collections             show collections in current database
	show users                   show users in current database
	show profile                 show most recent system.profile entries with time >= 1ms
	show logs                    show the accessible logger names
	show log [name]              prints out the last segment of log in memory, 'global' is default
	use <db_name>                set current database
	db.foo.find()                list objects in collection foo
	db.foo.find( { a : 1 } )     list objects in foo where a == 1
	it                           result of the last line evaluated; use to further iterate
	DBQuery.shellBatchSize = x   set default number of items to display on shell
	exit                         quit the mongo shell



> use testdb
switched to db testdb
> show collections
> show dbs
admin (empty)
local 0.078125GB
test (empty)
testdb (empty)
> db.testcoll.insert({name: "tom"})
> show collections
system.indexes
testcoll
> db.testcoll.find()
{ "_id" : ObjectId("575cd1692756c2ee1a25da27"), "name" : "tom" }
> db.testcoll.insert({name: 'jack'})
> db.testcoll.find()
{ "_id" : ObjectId("575cd1692756c2ee1a25da27"), "name" : "tom" }
{ "_id" : ObjectId("575cd1962756c2ee1a25da28"), "name" : "jack" }
> db.testcoll.insert({name: 'lily',age: 18},{name: 'lucy', age: 19})


显示状态:
> db.testcoll.stats()
{
"ns" : "testdb.testcoll",
"count" : 3,
"size" : 132,
"avgObjSize" : 44,
"storageSize" : 4096,
"numExtents" : 1,
"nindexes" : 1,
"lastExtentSize" : 4096,
"paddingFactor" : 1,
"systemFlags" : 1,
"userFlags" : 0,
"totalIndexSize" : 8176,
"indexSizes" : {
"_id_" : 8176
},
"ok" : 1
}


删除文档
> show collections
system.indexes
testcoll
> show dbs
admin (empty)
local 0.078125GB
test (empty)
testdb 0.203125GB
> db.testcoll.drop()
true
> show collections
system.indexes


创建新表
> use itpart
switched to db itpart
> db.worker.insert({name: 'jack'})
> db.worker.insert({name: 'tom'})
> db.worker.insert({name: 'lily',age: 22})
> db.worker.insert({name: 'lucy',age: 23})
> db.worker.find()
{ "_id" : ObjectId("575cd3922756c2ee1a25da2a"), "name" : "jack" }
{ "_id" : ObjectId("575cd39a2756c2ee1a25da2b"), "name" : "tom" }
{ "_id" : ObjectId("575cd3ac2756c2ee1a25da2c"), "name" : "lily", "age" : 22 }
{ "_id" : ObjectId("575cd3b72756c2ee1a25da2d"), "name" : "lucy", "age" : 23 }
> db.city.insert({area: 'shenzhen'})
> show collections
city
system.indexes
worker
> db.city.find()
{ "_id" : ObjectId("575cd3ef2756c2ee1a25da2e"), "area" : "shenzhen" }
> db.worker.find()
{ "_id" : ObjectId("575cd3922756c2ee1a25da2a"), "name" : "jack" }
{ "_id" : ObjectId("575cd39a2756c2ee1a25da2b"), "name" : "tom" }
{ "_id" : ObjectId("575cd3ac2756c2ee1a25da2c"), "name" : "lily", "age" : 22 }
{ "_id" : ObjectId("575cd3b72756c2ee1a25da2d"), "name" : "lucy", "age" : 23 }




查找年龄大于22的元素
> db.worker.find({age:{$gt: 22}})
{ "_id" : ObjectId("575cd3b72756c2ee1a25da2d"), "name" : "lucy", "age" : 23 }
只显示name
> db.worker.find({age: {$gt: 22}},{name:1 })
{ "_id" : ObjectId("575cd3b72756c2ee1a25da2d"), "name" : "lucy" }


修改元素:
> db.worker.update({age: {$gt: 18}}, {$set: { status: 'A'}},{multi: true})
> db.worker.find()
{ "_id" : ObjectId("575cd3922756c2ee1a25da2a"), "name" : "jack" }
{ "_id" : ObjectId("575cd39a2756c2ee1a25da2b"), "name" : "tom" }
{ "_id" : ObjectId("575cd4f52756c2ee1a25da2f"), "age" : 22, "name" : "jack", "status" : "A" }
{ "_id" : ObjectId("575cd3ac2756c2ee1a25da2c"), "age" : 22, "name" : "lily", "status" : "A" }
{ "_id" : ObjectId("575cd3b72756c2ee1a25da2d"), "age" : 23, "name" : "lucy", "status" : "A" }


删除元素:
> db.worker.remove({status: 'B'})
> db.worker.find()
{ "_id" : ObjectId("575cd3922756c2ee1a25da2a"), "name" : "jack" }
{ "_id" : ObjectId("575cd39a2756c2ee1a25da2b"), "name" : "tom" }
{ "_id" : ObjectId("575cd3ac2756c2ee1a25da2c"), "age" : 22, "name" : "lily", "status" : "A" }
{ "_id" : ObjectId("575cd3b72756c2ee1a25da2d"), "age" : 23, "name" : "lucy", "status" : "A" }




批量插入数据:
> for(i=1;i<=100;i++) db.testcoll.insert({name: "user" + i,age: i,gender: 'm',preferbooks: ['first book','second book']})
显示前三行
> db.testcoll.find().limit(3)
移除 年龄为8的元素
> db.testcoll.remove({age: 8})
查找年龄大于93的元素
> db.testcoll.find({age: {$gte: 93}})
找出年龄在60~70之间的元素
> db.testcoll.find({$and: [{age: {$gte: 60}}, {age: {$lte: 70}}]})
{ "_id" : ObjectId("575cd8302756c2ee1a25da6b"), "name" : "user60", "age" : 60, "gender" : "m", "preferbooks" : [  "first book",  "second book" ] }
{ "_id" : ObjectId("575cd8302756c2ee1a25da6c"), "name" : "user61", "age" : 61, "gender" : "m", "preferbooks" : [  "first book",  "second book" ] }
{ "_id" : ObjectId("575cd8302756c2ee1a25da6d"), "name" : "user62", "age" : 62, "gender" : "m", "preferbooks" : [  "first book",  "second book" ] }
{ "_id" : ObjectId("575cd8302756c2ee1a25da6e"), "name" : "user63", "age" : 63, "gender" : "m", "preferbooks" : [  "first book",  "second book" ] }
{ "_id" : ObjectId("575cd8302756c2ee1a25da6f"), "name" : "user64", "age" : 64, "gender" : "m", "preferbooks" : [  "first book",  "second book" ] }
{ "_id" : ObjectId("575cd8302756c2ee1a25da70"), "name" : "user65", "age" : 65, "gender" : "m", "preferbooks" : [  "first book",  "second book" ] }
{ "_id" : ObjectId("575cd8302756c2ee1a25da71"), "name" : "user66", "age" : 66, "gender" : "m", "preferbooks" : [  "first book",  "second book" ] }
{ "_id" : ObjectId("575cd8302756c2ee1a25da72"), "name" : "user67", "age" : 67, "gender" : "m", "preferbooks" : [  "first book",  "second book" ] }
{ "_id" : ObjectId("575cd8302756c2ee1a25da73"), "name" : "user68", "age" : 68, "gender" : "m", "preferbooks" : [  "first book",  "second book" ] }
{ "_id" : ObjectId("575cd8302756c2ee1a25da74"), "name" : "user69", "age" : 69, "gender" : "m", "preferbooks" : [  "first book",  "second book" ] }
{ "_id" : ObjectId("575cd8302756c2ee1a25da75"), "name" : "user70", "age" : 70, "gender" : "m", "preferbooks" : [  "first book",  "second book" ] }


显示address属性存在的元素
> db.testcoll.insert({name: 'jack',age: 28, address: 'shenzhen'})
> db.testcoll.find({address: {$exists: true}})
{ "_id" : ObjectId("575cdbf1c1e73f4b26e83033"), "name" : "jack", "age" : 28, "address" : "shenzhen" }
> db.testcoll.find({address: {$exists: false}})


修改元素,默认只修改第一行
> db.testcoll.update({age: {$gt: 80}} , {$set: {gender: 'f'}})
满足条件的全部修改
> db.testcoll.update({age: {$gt: 80}}, {$set: {gender: 'f'}},{multi: true})
删除列
> db.testcoll.update({name: 'user81'},{$unset: {preferbooks: ''}})
> db.testcoll.find({age: {$gt: 80}})
{ "_id" : ObjectId("575cd8302756c2ee1a25da80"), "age" : 81, "gender" : "f", "name" : "user81" }


索引的操作:

显示索引
> db.testcoll.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"ns" : "itpart.testcoll",
"name" : "_id_"
}
]


> db.testcoll.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"ns" : "itpart.testcoll",
"name" : "_id_"
},
{
"v" : 1,
"key" : {
"name" : 1
},
"ns" : "itpart.testcoll",
"name" : "name_1"
}
]


设置索引的方法
> db.testcoll.ensureIndex({name:'hashed'})
> db.testcoll.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"ns" : "itpart.testcoll",
"name" : "_id_"
},
{
"v" : 1,
"key" : {
"name" : 1
},
"ns" : "itpart.testcoll",
"name" : "name_1"
},
{
"v" : 1,
"key" : {
"name" : "hashed"
},
"ns" : "itpart.testcoll",
"name" : "name_hashed"
}
]


删除
> db.testcoll.dropIndex('name_hashed')
{ "nIndexesWas" : 3, "ok" : 1 }
> db.testcoll.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"ns" : "itpart.testcoll",
"name" : "_id_"
},
{
"v" : 1,
"key" : {
"name" : 1
},
"ns" : "itpart.testcoll",
"name" : "name_1"
}
]


显示查找过程
> db.testcoll.find({name:'user19'})
{ "_id" : ObjectId("575cd8302756c2ee1a25da42"), "name" : "user19", "age" : 19, "gender" : "m", "preferbooks" : [  "first book",  "second book" ] }
> db.testcoll.find({name:'user19'}).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" : [
[
"user19",
"user19"
]
]
},
"server" : "node2.chinasoft.com:27017"
}

posted @ 2016-06-17 20:56  reblue520  阅读(296)  评论(0编辑  收藏  举报