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 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的. 它是可扩展的高性能数据存储解决方案,经常被用于非关系型数据的存储,能存储海量的数据. 常 ...
随机推荐
- 超好用的Redis管理及监控工具,使用后可大大提高你的工作效率!
Redis做为现在web应用开发的黄金搭担组合,大量的被应用,广泛用于存储session信息,权限信息,交易作业等热数据.做为一名有10年以上JAVA开发经验的程序员,工作中项目也是广泛使用了Redi ...
- 团队开发冲刺2-----2day
冲刺目标: 1.在第一阶段的基础上完成app内部界面设计. 2.逐步完成app内每一部分内容. 3.对app的实现进一步仔细钻研考虑. 4.对app每一部分内容模块化,分工在进一步明确. 5.设计好数 ...
- [leetcode-582-Kill Process]
Given n processes, each process has a unique PID (process id) and its PPID (parent process id). Each ...
- Chapter 7. Design and Performance
本章将对MPEG4及H.264的实现细节进行讲解和比对. Motion Estimation 衡量运动估计的好坏有三种函数(第228页):MSE,MAE和SAE,其中由于SAE运算速度最快所以采用的最 ...
- MyEclipse 2014 破解补丁及激活步骤
针对 MyEclipse Trail Expired 问题的解决方法 我用网上找的注册类生成注册码的方式注册了一下,重启MyEclipse仍然会有提示弹窗,不过剩余时间由 4 days变成了 5 da ...
- Python系列教程(三):输入和输出
1.1 raw_input() 在Python中,获取键盘输入的数据的方法是采用 raw_input 函数(至于什么是函数,咱们以后的章节中讲解),那么这个 raw_input 怎么用呢? 看如下示例 ...
- JavaScript 第一课
今天进入到了js的阶段,了解到了JavaScript是一个很重要的阶段,所以要好好的理清每一个知识点 JavaScript的使用: 在<head>标签里应用<script> ...
- 同一个tomcat多个项目共享session,一个tomcat两个项目共享sessionId
同一个tomcat多个项目共享session,一个tomcat两个项目共享sessionId >>>>>>>>>>>>>& ...
- MySQL 'localhost' (10061)解决方法
今天启动mysql 出现10061错误,处理:修改hosts文件中localhost指向127.0.0.1. 参看:http://www.cnblogs.com/ljian/archive/2011/ ...
- OpenStack(企业私有云)万里长征第五步——虚拟机Migrate&Resize
一.前言 上一篇文章讲了OpenStack的部署和简单操作,今天介绍一下如何实现虚拟机的Migrate以及Resize.Migrate操作和Resize操作基本上属于同一种操作,Migrate操作只是 ...