客户端和服务端的安装

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

}

mongodb数据库安装及常见操作的更多相关文章

  1. python操作三大主流数据库(8)python操作mongodb数据库②python使用pymongo操作mongodb的增删改查

    python操作mongodb数据库②python使用pymongo操作mongodb的增删改查 文档http://api.mongodb.com/python/current/api/index.h ...

  2. mongoVUE破解与配置、Mongodb数据库安装

    一.mongoVUE 1.5.3破解: 1) 开始-运行-regedit-搜索:B1159E65-821C3-21C5-CE21-34A484D54444 2.) 然后把1,2,3项数值删除,然后重新 ...

  3. mongodb的安装与简单操作

    MongoDB中文社区:http://www.mongoing.com     数据库的使用场景 SQL(关系型数据库):MySQL.SQLServer  --->磁盘操作 1.高度事务性的场景 ...

  4. MongoDB数据库安装及配置环境终极教程(windows10系统)

    本文是笔者花时间踩坑踩生气了写出来的!转载请注明出处@http://www.cnblogs.com/tim100/!请尊重我的劳动成果!谢谢! 今天,给大家说说在windows10系统下MongoDB ...

  5. mongodb数据库安装

    mongodb的安装 1,下载安装包: http://www.runoob.com/mongodb/mongodb-window-install.html 2,安装至:    D:\MongoDB,将 ...

  6. MongoDB数据库安装及配置环境(windows10系统)

    windows10系统下MongoDB的安装及环境配置: MongoDB的安装 下载地址: https://www.mongodb.com/download-center (这是windows10环境 ...

  7. 第十二节:Web爬虫之MongoDB数据库安装与数据存储

    MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案. MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功 ...

  8. MongoDB 数据库安装

    首先在官网上下载数据库:官网上提供了两种形式的数据库,一种是免安装版的,一种是安装版的.这点跟apache的tomcat类似,安装版的有可视化的界面对服务进行启动和关闭,可是还是比較喜欢免安装的.不解 ...

  9. MongoDB的安装及CURD操作

    MongoDB的下载地址:http://www.mongodb.org/downloads MongoDB有32bit和64bit两个版本,32bit只能存放2GB数据.我们选择64bit版进行下载. ...

随机推荐

  1. A1051. Pop Sequence

    Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and p ...

  2. 【LOJ#10154】选课

    题目中所说的每门课都可能有先修课,也可能没有先修课,因此课与课之间的关系构成了一颗森林. 这种树上选择若干物品的最优解问题对应着树形背包问题. 阶段:子树的大小 状态:在当前子树中,选取 i 门课能够 ...

  3. UML类关系(依赖,关联,聚合,组合,泛化,实现)

    转自https://blog.csdn.net/k346k346/article/details/59582926   在学习面向对象设计时,类关系涉及依赖.关联.聚合.组合和泛化这五种关系,耦合度依 ...

  4. 计算机网络原理和OSI模型与TCP模型

    计算机网络原理和OSI模型与TCP模型 一.计算机网络的概述 1.计算机网络的定义 计算机网络是一组自治计算机的互连的集合 2.计算机网络的基本功能 a.资源共享 b.分布式处理与负载均衡 c.综合信 ...

  5. 学习windows编程 day4 之 自定义映射

    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRU ...

  6. JavaScript中的栈和堆内存,作用域

    1.栈 stack”和“堆 heap”            简单的来讲,stack上分配的内存系统自动释放,heap上分配的内存,系统不释放,哪怕程序退出,那一块内存还是在那里.stack一般是静态 ...

  7. Oracle记录-开启与关闭数据库

    1.配置tnsnames.ora/listener.ora #cd /usr/oracle/oracle/product/11.2.0/db_1/network/admin ---切换到安装目录 #v ...

  8. python 小程序,输错三次密码锁定账户

    [root@sun ~]# cat 7.py #!/usr/bin/python # -*- coding=UTF-8 -*- usera_name = 'usera' usera_passwd = ...

  9. storm-starter 例子学习

    storm-starter 是 apache storm 针对初学者的一个用maven管理的项目示例.这篇文章记录storm-starter例子代码分析及执行. 参考:https://github.c ...

  10. CSS进阶之SASS入门指南

    CSS进阶之SASS入门指南         随着跟着公司学习项目的前端的推进,越来越对好奇了许久的SASS垂涎欲滴,哈哈,可能这个词使用不当,没有关系,就是对SASS有一股神秘的爱!好了,闲话不多说 ...