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"
}
mongodb数据库安装及常见操作的更多相关文章
- python操作三大主流数据库(8)python操作mongodb数据库②python使用pymongo操作mongodb的增删改查
python操作mongodb数据库②python使用pymongo操作mongodb的增删改查 文档http://api.mongodb.com/python/current/api/index.h ...
- mongoVUE破解与配置、Mongodb数据库安装
一.mongoVUE 1.5.3破解: 1) 开始-运行-regedit-搜索:B1159E65-821C3-21C5-CE21-34A484D54444 2.) 然后把1,2,3项数值删除,然后重新 ...
- mongodb的安装与简单操作
MongoDB中文社区:http://www.mongoing.com 数据库的使用场景 SQL(关系型数据库):MySQL.SQLServer --->磁盘操作 1.高度事务性的场景 ...
- MongoDB数据库安装及配置环境终极教程(windows10系统)
本文是笔者花时间踩坑踩生气了写出来的!转载请注明出处@http://www.cnblogs.com/tim100/!请尊重我的劳动成果!谢谢! 今天,给大家说说在windows10系统下MongoDB ...
- mongodb数据库安装
mongodb的安装 1,下载安装包: http://www.runoob.com/mongodb/mongodb-window-install.html 2,安装至: D:\MongoDB,将 ...
- MongoDB数据库安装及配置环境(windows10系统)
windows10系统下MongoDB的安装及环境配置: MongoDB的安装 下载地址: https://www.mongodb.com/download-center (这是windows10环境 ...
- 第十二节:Web爬虫之MongoDB数据库安装与数据存储
MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案. MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功 ...
- MongoDB 数据库安装
首先在官网上下载数据库:官网上提供了两种形式的数据库,一种是免安装版的,一种是安装版的.这点跟apache的tomcat类似,安装版的有可视化的界面对服务进行启动和关闭,可是还是比較喜欢免安装的.不解 ...
- MongoDB的安装及CURD操作
MongoDB的下载地址:http://www.mongodb.org/downloads MongoDB有32bit和64bit两个版本,32bit只能存放2GB数据.我们选择64bit版进行下载. ...
随机推荐
- jar的打包与共享
做成jar包的,有个弊处,打包apk时会暴露源码,当然也有解决方案,放在最后讲. 先来解释如何生成jar: 新建工程,将工程编译至无错,右键工程Export... 在弹出的提示框中选择Java文件夹下 ...
- 在Android中通过导入静态数据库来提高应用第一次的启动速度
一个Android应用给用户的第一印象非常重要,除了要有好的创意和美观的界面,性能也是很关键的部分,本文讨论的就是第一次启动的速度问题. Android应用的启动过程不能让用户等待太长时间,个人觉得最 ...
- noip2013火柴排队_Solution
要想对任意(ai,bi)和(aj和bj),当ai<aj时,都有bi<=bj:当ai>=aj时,bi>=bj,当对a进行升序排序后(b同时发生改变,从而不改变值,最后有a1& ...
- hdu 1114Piggy-Bank(完全背包)
传送门 https://www.cnblogs.com/violet-acmer/p/9852294.html 参考资料: [1]:https://www.cnblogs.com/jbelial/ar ...
- LINUX_正则表达式
『正规表示法与通配符是完全不一样的东西!』这很重要喔!因为『通配符 (wildcard) 代表癿是 bash 操作接口癿一个功能』,但正觃表示法则是一种字符串处理癿表示方式 . (小数点):代表『 ...
- pubmed_cookie 自动获取
Python爬虫视频教程零基础小白到scrapy爬虫高手-轻松入门 https://item.taobao.com/item.htm?spm=a1z38n.10677092.0.0.482434a6E ...
- MySQL中的编码问题
1.查看MySQL数据库的默认编码 (1).使用status命令 mysql> status -------------- mysql Ver 14.14 Distrib 5.5.28, for ...
- Create ISO library over NFS for XEN server templates
Based on Ubuntu – Server – install NFS on Ubuntu – aptitude -y install nfs-kernel-server create a “ ...
- 让div固定在顶部不随滚动条滚动【转】
让div固定在顶部不随滚动条滚动 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "h ...
- dubbo集群服务下一台服务挂了对服务调用的影响
一.问题描述:项目中2台dubbo服务给移动端提供查询接口,移动端反应说查询时而很快(秒刷),时而很慢(4-5秒). 二.问题分析: 1.问题猜想:网络不稳定原因导致,但是切换公司wifi和手机4G, ...