查看所有数据库

> 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入门命令的更多相关文章

  1. mongodb入门命令-创建表数据(二)

    1.mongodb入门命令 1.1 show databases; 或 show dbs; //查看当前的数据库 > show dbs; admin 0.000GB config 0.000GB ...

  2. MongoDB基础命令

    MongoDB 入门命令 查看当前数据库 > show dbs admin 0.000GB config 0.000GB local 0.000GB > -- use databaseNa ...

  3. mongodb入门很简单(3)

     ##简单的mongodb入门命令## 1.show dbs;      //查看当前数据库 2.use databaseName;         //选库 3.show tables/collec ...

  4. MongoDB 入门之基础 DCL

    此文章主要记录部分主要的 MongoDB 的 DCL 操作. MongoDB 默认不需要用户名和密码就可以用 mongodb.exe 登录 一.开启 MonogoDB 的权限模式 修改 MongoDB ...

  5. MongoDB入门三:MongoDB shell

    MongoDB shell MongDB shell是一个功能完备的Javascript解释器,可以运行Javascript程序.也可以用于连接MongoDB服务器,执行脚本,对数据库进行操作.类似于 ...

  6. MongoDB入门分享-笔记整理精选

    最近在学习MongoDB,怕以后忘记,自己做了一个整理,给不知道的小伙伴一起分享学习一下. 第一步> 首先到官网下载,安装MongoDB.(注意MongoDB还有一个可视化管理工具叫: Mong ...

  7. MongoDB入门简介

    MongoDB入门简介 http://blog.csdn.net/lolinzhang/article/details/4353699 有关于MongoDB的资料现在较少,且大多为英文网站,以上内容大 ...

  8. mongodb入门教程

    title: mongodb入门教程 date: 2016-04-06 14:47:18 tags: --- 为什么要认识呢,因为这玩意就一傻逼 借用一下百科的介绍 MongoDB 是一个介于关系数据 ...

  9. MongoDb 入门教程

    MongoDb 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的. 它是可扩展的高性能数据存储解决方案,经常被用于非关系型数据的存储,能存储海量的数据. 常 ...

随机推荐

  1. 如何给两个swiper建立关系

    单个swiper已经满足不了需求了. 各种花式轮播已经慢慢进入市场.swiper该如何立足,那么请看. <div class="swiper-container"> & ...

  2. PHP设计模式:抽象工厂

    示例代码详见https://github.com/52fhy/design_patterns 抽象工厂 抽象工厂(Abstract Factory)是应对产品族概念的.比如说,每个汽车公司可能要同时生 ...

  3. java架构师负载均衡、高并发、nginx优化、tomcat集群、异步性能优化、Dubbo分布式、Redis持久化、ActiveMQ中间件、Netty互联网、spring大型分布式项目实战视频教程百度网盘

    15套Java架构师详情 * { font-family: "Microsoft YaHei" !important } h1 { background-color: #006; ...

  4. Mysql 根据时间戳按年月日分组统计

    Mysql 根据时间戳按年月日分组统计create_time时间格式SELECT DATE_FORMAT(create_time,'%Y%u') weeks,COUNT(id) COUNT FROM ...

  5. 【LeetCode】220. Contains Duplicate III

    题目: Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  6. 【Android Developers Training】 64. 绘制形状

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  7. 遇到报ClassNotFoundException: Didn't find class "...Activity" on path: DexPathList

    有一个工程,本来运行是正常的,我想把它移植到另一台PC上,结果报: java.lang.RuntimeException: Unable to instantiate activity Compone ...

  8. 再谈AbstractQueuedSynchronizer:共享模式与基于Condition的等待/通知机制实现

    共享模式acquire实现流程 上文我们讲解了AbstractQueuedSynchronizer独占模式的acquire实现流程,本文趁热打铁继续看一下AbstractQueuedSynchroni ...

  9. 简单总结一下 XSS

    你听说过XSS吗? XSS(Cross-site scripting, 跨站脚本)是一种网站应用程序的安全漏洞攻击,是代码注入的一种. 研究表明,最近几年 XSS 已经超过 "缓冲区溢出&q ...

  10. 实时音视频互动系列(上):又拍云UTUN网络详解

    如何定义实时音视频互动, 延迟 400ms 内才能无异步感 实时音视频互动如果存在1秒左右的延时会给交流者带来异步感,必须将视频播放延迟限制在400ms以内,才能给用户较好的交互体验. 当延迟控制在4 ...