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版进行下载. ...
随机推荐
- CF 1023
昨天晚上打的一场CF,口胡一下前4题吧. A要注意细节,先找*,如果没有就判两者相等. 然后注意长度n - 1 <= m,然后前后比较,最后判断中间是不是字母. B先判断有没有解,然后求出 k ...
- Redis我想入门——启动
nosql数据库出现到现在很多年了.笔者一直从事C/S模式的上开发.所以相对而言笔者只是听过却从来不知道他是什么东西.时代在变化,当年所有业务都放在一个War包的时代已经不在了.微服务已经成为了世界主 ...
- 如何删除launchpad里的空文件夹
方法1: 重启后将任意一个应用拖入再移出 方法2: 在终端(应用工具>实用工具>终端)执行:defaults write com.apple.dock ResetLaunchPad -bo ...
- 一不小心用oracle关键子创建了表,如何修改
一不小心将备注字段建成了ORACLE的关键子COMMENT. 用下面的语句修改成NOTES ------------------------------------------------------ ...
- html5的data-*属性,我们一起认识下
html5的data-自定义属性出来很久了,我们一起认识一下. 比如如下一个代码,我们怎么取到对应的name,对应的age? 既然data-*自定义属性是它的一个属性,这个时候就可以用getAttri ...
- shell中脚本变量和函数变量的作用域
http://blog.csdn.net/ltx19860420/article/details/5570902 1. shell脚本中定义的变量是global的,其作用域从被定义的地方开始,到she ...
- & 引用传值
public function ko(){ $arr_1 = [ [], [], [], [], [] ]; $arr_2 = [ [], [], [], [] ]; foreach ($arr_1 ...
- brctl创建虚拟网卡详解
brctl创建虚拟网卡详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 很久之前我分享过一篇关于搭建Openvpn的笔记,在笔记的最后我分享了一个脚本,是用来创建虚拟网卡的,今天 ...
- Linux安装-kickstart无人值守安装
Linux安装-kickstart无人值守安装 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 大家做运维估计都避免不了时不时会去机房安装一台linux服务器,但是服务器启动的特别慢 ...
- 多目标遗传算法 ------ NSGA-II (部分源码解析)父、子种群合并 merge.c
/* Routine for mergeing two populations */ # include <stdio.h> # include <stdlib.h> # in ...