mongodb入门命令-创建表数据(二)
1.mongodb入门命令
1.1 show databases; 或 show dbs; //查看当前的数据库
> show dbs;
admin 0.000GB
config 0.000GB
local 0.000GB
1.2 use databaseName 选择库
show tables/collections 查看当前库下的collections
1.3 如何创建库
mongodb 的库是隐式创建,你可以use一个不存在的库
然后在该库下创建collection,即可创建库
1.4 db.createCollection('collectionName'); //创建collection
1.5 collection 允许隐式创建
db.collectionName.insert(document);
1.6 db.collectionName.drop(); /删除collection
> use shop
switched to db shop
> db.createCollection('user');
{ "ok" : 1 }
> show dbs;
admin 0.000GB
config 0.000GB
local 0.000GB
shop 0.000GB
> show collections;
user
1.7插入user表语句
(1)自动生成id值
db.user.insert({name:'lisi',age:22})
WriteResult({ "nInserted" : 1 })
> db.user.find();
{ "_id" : ObjectId("5d73077c71b815674de4d152"), "name" : "lisi", "age" : 22 }
(2) 指定生成id
> db.user.insert({_id:2,name:'wangwu',age:25})
WriteResult({ "nInserted" : 1 })
> db.user.find();
{ "_id" : ObjectId("5d73077c71b815674de4d152"), "name" : "lisi", "age" : 22 }
{ "_id" : 2, "name" : "wangwu", "age" : 25 }
(3) 插入多层
> db.user.insert({_id:3,name:'xiaobing',hobby:['basketball','football'],intro:{'title':'My intro','content':'from china'}});
WriteResult({ "nInserted" : 1 })
> db.user.find();
{ "_id" : ObjectId("5d73077c71b815674de4d152"), "name" : "lisi", "age" : 22 }
{ "_id" : 2, "name" : "wangwu", "age" : 25 }
{ "_id" : 3, "name" : "xiaobing", "hobby" : [ "basketball", "football" ], "intro" : { "title" : "My intro", "content" : "from china" } }
>
1.8 其实mongodb不需要声明表,可直接写入表数据,即可创建成功!
> show tables;
user
> db.goods.insert({_id:1,name:'oppoR11',price:'3000'});
WriteResult({ "nInserted" : 1 })
> show tables;
goods
user
> db.goods.find()
{ "_id" : 1, "name" : "oppoR11", "price" : "3000" }
1.9 删除表 db.collectionName.drop();
> show collections
goods
user
> db.goods.drop();
true
> show collections;
user
>
2.0 删除数据库
db.dropDatabase()
> show dbs;
admin 0.000GB
config 0.000GB
local 0.000GB
shop 0.000GB
> use shop;
switched to db shop > db.dropDatabase();
{ "dropped" : "shop", "ok" : 1 }
> show dbs;
admin 0.000GB
config 0.000GB
local 0.000GB
>
2.1 查询帮助 db.help();
> db.help()
DB methods:
db.adminCommand(nameOrDocument) - switches to 'admin' db, and runs command [just calls db.runCommand(...)]
db.aggregate([pipeline], {options}) - performs a collectionless aggregation on this database; returns a cursor
db.auth(username, password)
db.cloneDatabase(fromhost) - deprecated
db.commandHelp(name) returns the help for the command
db.copyDatabase(fromdb, todb, fromhost) - deprecated
db.createCollection(name, {size: ..., capped: ..., max: ...})
db.createView(name, viewOn, [{$operator: {...}}, ...], {viewOptions})
db.createUser(userDocument)
db.currentOp() displays currently executing operations in the db
db.dropDatabase()
db.eval() - deprecated
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([filter]) - returns a list that contains the names and options of the db's collections
db.getCollectionNames()
db.getLastError() - just returns the err msg string
db.getLastErrorObj() - return full status object
db.getLogComponents()
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.setLogLevel(level,<component>)
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
未完,后续补充....
mongodb入门命令-创建表数据(二)的更多相关文章
- oracle用命令创建表空间、用户,并为用户授权、收回权限。
oracle中如何删除用户? Oracle中使用DROP USER来删除用户,如果使用DROP USER CASCADE那么用户的对象也同时被删除掉.为了达到删除用户的效果而又不影响对用户下的对象的使 ...
- MongoDB使用命令创建用户权错误分析--- 权限不够Error:couldn't add user:command createUser requires authentication
MongoDB使用命令创建用户权错误分析 错误一:权限不够Error:couldn't add user:command createUser requires authentication. 解决方 ...
- HBase shell 命令创建表及添加数据操作
创建表,表名hbase_1102,HBase表是由Key-Value组成的,此表中Key为NAME 此表有两个列族,CF1和CF2,其中CF1和CF2下分别有两个列name和gender,Chin ...
- Hive创建表|数据的导入|数据导出的几种方式
* Hive创建表的三种方式 1.使用create命令创建一个新表 例如:create table if not exists db_web_data.track_log(字段) partitione ...
- MongoDB 基础命令——数据库表的增删改查——遍历操作表中的记录
分组排序查询最大记录 //对 "catagory" 不等于 null 的数据进行分组查询,且查询结果倒序 db.getCollection('userAccount').aggre ...
- mysql 如何用命令清除表数据,让表数据索引是从0开始呢?
truncate MYTABLE 这样就可以了 其实这个命令就相当于删除表再建 所有的数据都还原 可以使用工具来完成这个操作 右键单击要操作的表,选择Turncale Table 执行查询语句,数据就 ...
- 9.MongoDB系列之创建副本集(二)
1. 如何设计副本集 大多数:选取主节点时需要由大多数决定,主节点只有在得到大多数支持时才能继续作为主节点,写操作被复制到大多数成员时就是安全的写操作.这里的大多数定义为"副本集中一半以上的 ...
- MongoDB入门命令
查看所有数据库 > show dbs admin (empty) local 0.078GB > admin和管理相关, admin和local数据库不要轻易动 选择库 > use ...
- Jqgrid入门-操作表格的数据(二)
上一篇中,Jqgrid已经可以从服务端获得数据,并显示在Grid表格中了.下面说一下,如何操作表格及其数据. jqGrid有很多方法函数,用来操作数据或者操作Grid表格本身.jq ...
随机推荐
- Jmeter系列(24)- 常用逻辑控制器(3) | 模块控制器Module Controller
模块控制器(Module Controller) 作用 可以理解为引用.调用执行的意思,调用范围为testplan树下任意的逻辑控制器,模块控制器除外 点开testplan树,需要引用哪个逻辑控制器选 ...
- 配置阿里云maven
在安装好Maven之后,默认的~/.m2目录下是没有maven仓库配置文件settings.xml的,默认使用的是官方的仓库,访问速度会非常慢,我们需要配置下国内的仓库. 创建~/.m2/settin ...
- 不使用插件的ajax 上传文件
参考博主资料 我没试 我使用了带js的插件的方式 https://www.cnblogs.com/pegasus827/p/10941775.html 第一步:html form表单代码 第二步 ...
- vue three.js 结合tween.js 实现动画过渡
参考地址:https://www.jianshu.com/p/d6e3b4b153bb https://www.jqhtml.com/10513.html 官方文档:https://github.co ...
- CF618F-Double Knapsack【结论】
正题 题目链接:https://www.luogu.com.cn/problem/CF618F 题目大意 给出大小为\(n\),值域为\([1,n]\)的两个可重集合\(A,B\) 需要你对它们各求出 ...
- 常见JS
1.获取当前月份第一天 var date = new Date(); var year = date.getFullYear(); var month = date.getMonth() + 1; v ...
- 启用 Spring-Cloud-OpenFeign 配置可刷新,项目无法启动,我 TM 人傻了(上)
本篇文章涉及底层设计以及原理,以及问题定位,比较深入,篇幅较长,所以拆分成上下两篇: 上:问题简单描述以及 Spring Cloud RefreshScope 的原理 下:当前 spring-clou ...
- CSharp委托与匿名函数
CSharp委托与匿名函数 场景 面对事件处理,我们通常会通过定义某一个通用接口,在该接口中定义方法,然后在框架代码中,调用实现该接口的类实例的方法来实现函数的回调.可能这样来说有些抽象,那我们提供一 ...
- Spring自动装配歧义性笔记
Spring自动装配歧义性笔记 如果系统中存在两个都实现了同一接口的类,Spring在进行@Autowired自动装配的时候,会选择哪一个?如下: // 一下两个类均被标记为bean @Compone ...
- storm卡顿修改
最近的webstorm越来越卡了,有时候甚至会弹出 Out of memory的窗口,提示要设置 xmx的值, 8G内存跑你这小软件还会不够用???要内存?给你,看你还会不会卡成翔! 于是果断给x ...