首先我们先安装这个数据库。你能够使用windows或者linux,但推荐使用的是linux,我使用的是ubuntu12.04.在下方的网址中共能够下载,基本都是64位的系统。

假设位linux系统也能够使用命令行安装,我就是使用sudo apt-get install mongodb。

https://www.mongodb.org/downloads

MongoDB 由 databases 组成,databases 由 collections 组成,collections 由documents(相当于行)组成,而 documents 有 fields(相当于列)组成。

help

最開始应该介绍的就是help函数。学习一个东西最好的文档就是官方文档。以下我会给出几个指令的运行过程。最主要的形式就是db.help(),也能够在中间加上数据库的名称如db.douban.help()。

> db.help()
DB methods:
db.addUser(username, password[, readOnly=false])
db.auth(username, password)
db.cloneDatabase(fromhost)
db.commandHelp(name) returns the help for the command
db.copyDatabase(fromdb, todb, fromhost)
db.createCollection(name, { size : ..., capped : ..., max : ... } )
db.currentOp() displays the current operation in the db
db.dropDatabase()
db.eval(func, args) run code server-side
db.getCollection(cname) same as db['cname'] or db.cname
db.getCollectionNames()
db.getLastError() - just returns the err msg string
db.getLastErrorObj() - return full status object
db.getMongo() get the server connection object
db.getMongo().setSlaveOk() allow this connection to read from the nonmaster member of a replica pair
db.getName()
db.getPrevError()
db.getProfilingLevel() - deprecated
db.getProfilingStatus() - returns if profiling is on and slow threshold
db.getReplicationInfo()
db.getSiblingDB(name) get the db at the same server as this one
db.isMaster() check replica primary status
db.killOp(opid) kills the current operation in the db
db.listCommands() lists all the db commands
db.logout()
db.printCollectionStats()
db.printReplicationInfo()
db.printSlaveReplicationInfo()
db.printShardingStatus()
db.removeUser(username)
db.repairDatabase()
db.resetError()
db.runCommand(cmdObj) run a database command. if cmdObj is a string, turns it into { cmdObj : 1 }
db.serverStatus()
db.setProfilingLevel(level,<slowms>) 0=off 1=slow 2=all
db.shutdownServer()
db.stats()
db.version() current version of the server
db.getMongo().setSlaveOk() allow queries on a replication slave server
db.fsyncLock() flush data to disk and lock server for backups
db.fsyncUnock() unlocks server following a db.fsyncLock()
> db.douban.help()
DBCollection help
db.douban.find().help() - show DBCursor help
db.douban.count()
db.douban.dataSize()
db.douban.distinct( key ) - eg. db.douban.distinct( 'x' )
db.douban.drop() drop the collection
db.douban.dropIndex(name)
db.douban.dropIndexes()
db.douban.ensureIndex(keypattern[,options]) - options is an object with these possible fields: name, unique, dropDups
db.douban.reIndex()
db.douban.find([query],[fields]) - query is an optional query filter. fields is optional set of fields to return.
e.g. db.douban.find( {x:77} , {name:1, x:1} )
db.douban.find(...).count()
db.douban.find(...).limit(n)
db.douban.find(...).skip(n)
db.douban.find(...).sort(...)
db.douban.findOne([query])
db.douban.findAndModify( { update : ... , remove : bool [, query: {}, sort: {}, 'new': false] } )
db.douban.getDB() get DB object associated with collection
db.douban.getIndexes()
db.douban.group( { key : ..., initial: ..., reduce : ...[, cond: ...] } )
db.douban.mapReduce( mapFunction , reduceFunction , <optional params> )
db.douban.remove(query)
db.douban.renameCollection( newName , <dropTarget> ) renames the collection.
db.douban.runCommand( name , <options> ) runs a db command with the given name where the first param is the collection name
db.douban.save(obj)
db.douban.stats()
db.douban.storageSize() - includes free space allocated to this collection
db.douban.totalIndexSize() - size in bytes of all the indexes
db.douban.totalSize() - storage allocated for all data and indexes
db.douban.update(query, object[, upsert_bool, multi_bool])
db.douban.validate( <full> ) - SLOW
db.douban.getShardVersion() - only for use with sharding
db.douban.getShardDistribution() - prints statistics about data distribution in the cluster

use

use deng能够用来创建 deng,不用操心 deng 不会创建,当创建第一个 collection 时,deng会自己主动创建。

insert

db.unicorns.insert({name: 'demo', sex: 'm', weight: 70}),插入一个数据 collection 为 unicorns。使 用 db.getCollectionNames()
, 会 得 到 unicorns 和 system.indexes 。system.indexes 对每一个 DB 都会有,用于记录 index。

能够通过find函数查看是否插入成功。

find

db.unicorns.find()会看到 document。就相当于mysql中的select * from table; 查看集合中的全部内容。

使用查询
db.unicorns.find({name: 'Dunx'})
 
其它说明:
$lt, $lte, $gt, $gte and $ne 分别表示小于、小于等于、大于、大于等于、不等于
db.unicorns.find({gender: {$ne: 'f'}, weight: {$gte: 701}}) $exists 用于表示 field 是否存在
db.unicorns.find({vampires: {$exists: false}}) or 和 and
db.unicorns.find({gender: 'f', $or: [{loves: 'apple'}, {loves: 'orange'}, {weight: {$lt: 500}}]})

db.unicorns.find(null, {name: 1})

仅仅返回 name 这个 field,详细例如以下所看到的:

> db.unicorns.find(null, {name: 1})
{ "_id" : ObjectId("4da6f22da8d5cd3b72081cf7"), "name" : "Aurora" }
{ "_id" : ObjectId("4da6f22da8d5cd3b72081cf6"), "name" : "Horny" }
{ "_id" : ObjectId("4da6f22da8d5cd3b72081cf8"), "name" : "Unicrom" }
{ "_id" : ObjectId("4da6f22da8d5cd3b72081cf9"), "name" : "Roooooodles" }
{ "_id" : ObjectId("4da6f22da8d5cd3b72081cfa"), "name" : "Solnara" }
{ "_id" : ObjectId("4da6f22da8d5cd3b72081cfb"), "name" : "Ayna" }
{ "_id" : ObjectId("4da6f22da8d5cd3b72081cfc"), "name" : "Kenny" }
{ "_id" : ObjectId("4da6f22da8d5cd3b72081cfd"), "name" : "Raleigh" }
{ "_id" : ObjectId("4da6f22da8d5cd3b72081cfe"), "name" : "Leia" }
{ "_id" : ObjectId("4da6f22da8d5cd3b72081cff"), "name" : "Pilot" }
{ "_id" : ObjectId("4da6f22da8d5cd3b72081d00"), "name" : "Nimue" }
{ "_id" : ObjectId("4da6f231a8d5cd3b72081d01"), "name" : "Dunx" }
> db.unicorns.find(null, {name: 1,_id:0})
{ "name" : "Aurora" }
{ "name" : "Horny" }
{ "name" : "Unicrom" }
{ "name" : "Roooooodles" }
{ "name" : "Solnara" }
{ "name" : "Ayna" }
{ "name" : "Kenny" }
{ "name" : "Raleigh" }
{ "name" : "Leia" }
{ "name" : "Pilot" }
{ "name" : "Nimue" }
{ "name" : "Dunx" }

sort

db.unicorns.find().sort({weight: -1})
db.unicorns.find().sort({name: 1, vampires: -1})
#1 表示升序,-1 表示降序 db.unicorns.find().sort({weight: -1}).limit(2).skip(1)
#得到第二个和第三个,limit 规定查询个数,skip 规定忽略几个。

count

db.unicorns.count({vampires: {$gt: 50}})
#or
db.unicorns.find({vampires: {$gt: 50}}).count()

remove

db.unicorns.remove()
如以下所看到的。 > db.unicorns.remove()
> db.unicorns.find()
>

update

db.unicorns.update({name: 'jingdong'}, {weight: 590})

注意:此语句运行后,先查询 name 是'jingdong'的全部数据,然后将 name是'jingdong'的整个 document 都替换为{weight: 590}。

即 db.unicorns.insert({name: 'jingdong', dob: new Date(1979, 7, 18, 18, 44),loves:['apple'], weight: 575, gender: 'm', vampires: 99});整个替换为{weight: 590},最后仅仅剩一个{weight: 590}。

运行$set,不会替换原有数据,因此正确的更新方式例如以下:

db.unicorns.update({name: 'jingdong'}, {$set: {weight: 590}})
db.unicorns.update({name: 'Pilot'}, {$inc: {<span style="font-size:14px;">weight: 500</span>}})
$inc 添加或降低数字 假设原来为500之后会变为1000,由于500+500 = 1000 db.unicorns.update({name: 'Aurora'}, {$push: {loves: 'sugar'}})
$push 添加数组元素
$pop 降低数组元素
若存在则更新,否则加入
db.hits.update({page: 'unicorns'}, {$inc: {hits: 1}}, true);
db.hits.find();
使用第三个參数设置是否 true(upset)
,默认是 false
#批量更新
db.unicorns.update({}, {$set: {vaccinated: true }});
db.unicorns.find({vaccinated: true});
不会将全部的数据的 vaccinated 都更新为 true
若将全部的数据的 vaccinated 都更新为 true,则例如以下:
db.unicorns.update({}, {$set: {vaccinated: true }}, false, true);
db.unicorns.find({vaccinated: true});

ensureIndex

创建索引的方式

db.unicorns.ensureIndex({name: 1})

删除索引的方式

db.unicorns.dropIndex({name: 1})

创建独立索引

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

创建联合索引

db.unicorns.dropIndex({name: 1, vampires: -1})

使用 web 获得 mongoDB 的信息



使用 http://localhost:28017/ 获得 MongoDB 的信息。

数据备份和恢复

使用 mongodump备份数据库

mongodump

使用 mongorestore恢复数据库

mongorestore

MongoDB经常使用命令的更多相关文章

  1. 前端开发小白必学技能—非关系数据库又像关系数据库的MongoDB快速入门命令(2)

    今天给大家道个歉,没有及时更新MongoDB快速入门的下篇,最近有点小忙,在此向博友们致歉.下面我将简单地说一下mongdb的一些基本命令以及我们日常开发过程中的一些问题.mongodb可以为我们提供 ...

  2. mongodb与mysql命令对比

    mongodb与mysql命令对比 传统的关系数据库一般由数据库(database).表(table).记录(record)三个层次概念组成,MongoDB是由数据库(database).集合(col ...

  3. [整理]MongoDB 经常使用命令总结

    MongoDB 经常使用命令总结 简单的的增删改查数据 在查询结果中指定显示或者不显示某个字段 比如,我们希望在 lessons 集合中查找全部数据,可是不希望在返回结果中包括 slides 字段:由 ...

  4. 解决mongodb设备mongod命令不是内部或外部的命令

    1:安装 去mongodb的官网http://www.mongodb.org/downloads下载32bit的包 解压后会出现下面文件 在安装的盘C:下建立mongodb目录,拷贝bin目录到该目录 ...

  5. MongoDB学习笔记-命令

    连接数据库: mongodb://账号:密码@IP/库名 更多方式参考:http://www.runoob.com/mongodb/mongodb-connections.html 命令整理: 名称 ...

  6. mongodb 常用的命令

    mongodb 常用的命令 对数据库的操作,以及登录 1 进入数据库 use admin 2 增加或修改密码 db.addUser('wsc', '123') 3查看用户列表 db.system.us ...

  7. Mongodb的mongostat命令

    Mongodb的mongostat命令可实时(1秒钟刷新一次)显示Mongodb数据库的运行情况,可视为性能监视器. 1.启动命令:authenticationDatabase表示用户认证证书所在的数 ...

  8. mongodb输错命令后不能删除问题

    在用crt连接Linux操作MongoDB时,命令输错了,想删除的时候,却删除不了,原因是crt的配置有问题,解决办法如下 第一步:选项-->会话选项

  9. 【mongodb】——常用命令大全

    MongoDB是NoSQL数据库系统中比较流行的数据库之一.它也是最接近关系型数据库的,一个数据库可以包含多个集合(Collection),类似于关系数据库中的表:而每个集合中可以存储一组由列标识的记 ...

  10. MongoDB基本管理命令 [转]

    MongoDB基本管理命令 linux下配置安装mongodb 10分钟玩转mongoDB 官网安装教程 使用命令行方式连接mongodb: mongo /admin -u用户名 -p密码  --连接 ...

随机推荐

  1. Leetcode 335.路径交叉

    路径交叉 给定一个含有 n 个正数的数组 x.从点 (0,0) 开始,先向北移动 x[0] 米,然后向西移动 x[1] 米,向南移动 x[2] 米,向东移动 x[3] 米,持续移动.也就是说,每次移动 ...

  2. Linux下dpkg的用法

    转自:http://blog.csdn.net/fireblue1990/article/details/52627952 dpkg是一个Debian的一个命令行工具,它可以用来安装.删除.构建和管理 ...

  3. shit layui & bugs

    shit layui & bugs use is not useful at all! http://www.layui.com/demo/form.html layui.use([" ...

  4. [BZOJ2393] Cirno的完美算数教室(dfs+容斥原理)

    传送门 先通过dfs预处理出来所有只有2和9的数,也就大概2000多个. 想在[L,R]中找到是这些数的倍数的数,可以通过容斥原理 那么如果a % b == 0,那么便可以把 a 去掉,因为 b 的倍 ...

  5. 什么是JNI?

    JNI是Java Native Interface的缩写,它提供了若干的API实现了Java和其他语言的通信(主要是C和C++)

  6. VMware虚拟机 NAT模式 配置静态ip

    前言:Ubuntu 16.04 VMware虚拟机 NAT模式 配置静态ip,这个问题困扰我好长时间,桥接的静态ip我会了,然而用NAT 的方式配置集群会更好.(NAT 方式客户机之间的通讯不经过路由 ...

  7. 数字串(codevs 1394)

    题目描述 Description 给你一个长度为n的数字串,数字串里会包含1-m这些数字.如果连续的一段数字子串包含了1-m这些数字,则称这个数字字串为NUM串.你的任务是求出长度最短的NUM串是什么 ...

  8. 洛谷 [P3265] 装备购买

    线性基 通过题目描述可以感觉到就是要求线性基, 线性基的求法是高斯消元,消完以后剩下的x的系数非 0 的就是线性基 本题有一个贪心策略,每次挑选价格最小的来消掉其他的元 //可以快排预处理 #incl ...

  9. [MFC] CFile读写文件实现(高效)

    1.文件写入 void CMFCApplication1Dlg::Write() { CFile file; CString FileName = "D:\\100w.txt"; ...

  10. 有向图tarjan算法求连通分量的粗浅讲解、证明, // hdu1269

    打算开始重新复习一遍相关算法.对于有向图tarjan算法,通过学习过很多说法,结合自己的理解,下面给出算法自己的观点. 算法总模型是一个dfs,结合一个stack(存放当前尚未形成SCC的点集合),记 ...