MongoDB入门命令
查看所有数据库
> show dbs
admin (empty)
local 0.078GB
>
admin和管理相关, admin和local数据库不要轻易动
选择库
> use test #如果没有则创建
switched to db test
>
> db #查看当前位于哪个数据库
test
>
>
> show collections
>
db.help()请求帮助
> db.help()
DB methods:
db.adminCommand(nameOrDocument) - switches to 'admin' db, and runs command [ just calls db.runCommand(...) ]
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.createUser(userDocument)
db.currentOp() displays currently executing operations in the db
db.dropDatabase()
db.eval(func, args) run code server-side
db.fsyncLock() flush data to disk and lock server for backups
db.fsyncUnlock() unlocks server following a db.fsyncLock()
db.getCollection(cname) same as db['cname'] or db.cname
db.getCollectionInfos()
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 queries on a replication slave server
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.getWriteConcern() - returns the write concern used for any operations on this db, inherited from server object if set
db.hostInfo() get details about the server's host
db.isMaster() check replica primary status
db.killOp(opid) kills the current operation in the db
db.listCommands() lists all the db commands
db.loadServerScripts() loads all the scripts in db.system.js
db.logout()
db.printCollectionStats()
db.printReplicationInfo()
db.printShardingStatus()
db.printSlaveReplicationInfo()
db.dropUser(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.setWriteConcern( <write concern doc> ) - sets the write concern for writes to the db
db.unsetWriteConcern( <write concern doc> ) - unsets the write concern for writes to the db
db.setVerboseShell(flag) display extra information in shell output
db.shutdownServer()
db.stats()
db.version() current version of the server
>
创建数据库, 没有必要去创建collections, 可以隐式创建
> use demo #没有则创建
switched to db demo
>
> db.createCollection('user')
{ "ok" : 1 }
>
> show collections
system.indexes
user
>
> show dbs
admin (empty)
demo 0.078GB
local 0.078GB
test (empty)
>
插入数据
> db.user.insert({'name': 'lisi', age: 22})
WriteResult({ "nInserted" : 1 })
>
> db.user.find()
{ "_id" : ObjectId("572f495e3c8cf279cf89ad16"), "name" : "lisi", "age" : 22 }
>
>
自动生成id, 是当做主键用的, 也可以自定义id
> db.user.insert({_id:1, name:'jack', age:19})
WriteResult({ "nInserted" : 1 })
>
> db.user.find()
{ "_id" : ObjectId("572f495e3c8cf279cf89ad16"), "name" : "lisi", "age" : 22 }
{ "_id" : 1, "name" : "jack", "age" : 19 }
>
> db.user.insert({_id:3, name:'ql', hobby:['basketball', 'football'], intro: {'title': 'my intro'}})
WriteResult({ "nInserted" : 1 })
>
> db.user.find()
{ "_id" : ObjectId("572f495e3c8cf279cf89ad16"), "name" : "lisi", "age" : 22 }
{ "_id" : 1, "name" : "jack", "age" : 19 }
{ "_id" : 3, "name" : "ql", "hobby" : [ "basketball", "football" ], "intro" : { "title" : "my intro" } }
>
直接创建collection, 不用事先声明
> db.goods.insert{name:'ql'}
WriteResult({ "nInserted" : 1 })
>
>
> show collections
goods
system.indexes
user
>
把文档赋给一个变量, 然后插入值
> doc={name:'ql', age:29}
{ "name" : "ql", "age" : 29 }
>
> db.stu.insert(doc)
WriteResult({ "nInserted" : 1 })
>
> db.stu.find()
{ "_id" : ObjectId("572f4e8b3c8cf279cf89ad18"), "name" : "ql", "age" : 29 }
>
删除collection
> show collections
goods
system.indexes
user
>
>
> db.user.drop()
true
>
> show collections
goods
system.indexes
>
删除数据库
> db
demo
>
> db.dropDatabase()
{ "dropped" : "demo", "ok" : 1 }
>
> show dbs
admin (empty)
local 0.078GB
test (empty)
>
MongoDB入门命令的更多相关文章
- mongodb入门命令-创建表数据(二)
1.mongodb入门命令 1.1 show databases; 或 show dbs; //查看当前的数据库 > show dbs; admin 0.000GB config 0.000GB ...
- MongoDB基础命令
MongoDB 入门命令 查看当前数据库 > show dbs admin 0.000GB config 0.000GB local 0.000GB > -- use databaseNa ...
- mongodb入门很简单(3)
##简单的mongodb入门命令## 1.show dbs; //查看当前数据库 2.use databaseName; //选库 3.show tables/collec ...
- MongoDB 入门之基础 DCL
此文章主要记录部分主要的 MongoDB 的 DCL 操作. MongoDB 默认不需要用户名和密码就可以用 mongodb.exe 登录 一.开启 MonogoDB 的权限模式 修改 MongoDB ...
- MongoDB入门三:MongoDB shell
MongoDB shell MongDB shell是一个功能完备的Javascript解释器,可以运行Javascript程序.也可以用于连接MongoDB服务器,执行脚本,对数据库进行操作.类似于 ...
- MongoDB入门分享-笔记整理精选
最近在学习MongoDB,怕以后忘记,自己做了一个整理,给不知道的小伙伴一起分享学习一下. 第一步> 首先到官网下载,安装MongoDB.(注意MongoDB还有一个可视化管理工具叫: Mong ...
- MongoDB入门简介
MongoDB入门简介 http://blog.csdn.net/lolinzhang/article/details/4353699 有关于MongoDB的资料现在较少,且大多为英文网站,以上内容大 ...
- mongodb入门教程
title: mongodb入门教程 date: 2016-04-06 14:47:18 tags: --- 为什么要认识呢,因为这玩意就一傻逼 借用一下百科的介绍 MongoDB 是一个介于关系数据 ...
- MongoDb 入门教程
MongoDb 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的. 它是可扩展的高性能数据存储解决方案,经常被用于非关系型数据的存储,能存储海量的数据. 常 ...
随机推荐
- jQuery.validate 的form校验
jQuery验证框架 : 基本html代码: <script src="js/jquery-1.9.1.js"></script> <script s ...
- 读Zepto源码之属性操作
这篇依然是跟 dom 相关的方法,侧重点是操作属性的方法. 读Zepto源码系列文章已经放到了github上,欢迎star: reading-zepto 源码版本 本文阅读的源码为 zepto1.2. ...
- LinCode 刷题 之二叉树最小深度
http://www.lintcode.com/zh-cn/problem/minimum-depth-of-binary-tree/ 题目描述信息 /** * Definition of Tree ...
- (cljs/run-at (JSVM. :browser) "搭建刚好可用的开发环境!")
前言 书接上一回,在了解cljs基本语法后并在clojurescript.net的奇特错误提示后,我们必须痛定思痛地搭建一个本地的开发环境,以便后续深入地学习cljs. 现有的构建工具 由于浏览器 ...
- 数组 list互转
数组 list互转 String str[] = list.toArray(new String[]{}); List list= java.util.Arrays.asList(String str ...
- 8.javaweb之session
session是客户端和服务端的一次会话 web的session是指用户在浏览某个网站时,从进入网站到关闭浏览器的这段时间,uyejiushi用户浏览这个网站所花费的时间. session是一个时间的 ...
- php的修改
修改数据: (1)如果不是有外键的表,那么修改就正常的进行修改页面和修改处理页面就可以,但是有了外键的表,在进行遍历时就要写相应的条件了. 例如,有张表中有性别,但是进行创建表时是用的0或1来表示的, ...
- Ionic 常用组件解析
Ionic 常用组件解析 $ionicModal(弹出窗口): //创建一个窗口 //此处注意目录的起始位置为app $ionicModal.fromTemplateUrl('app/security ...
- H3CNE实验:配置VLAN和VLAN端口
配置准备数据: | 设备名称 | IP地址 | VLAN网关 | 接口 | VLAN | |---------------|--------------|----------------|------ ...
- mysql性能监控工具
参考文档: http://www.linuxidc.com/Linux/2012-09/70459.htm 1.记录慢查询SQL #配置开启 (linux)修改my.cnf: log-slow-que ...