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入门命令-创建表数据(二)的更多相关文章

  1. oracle用命令创建表空间、用户,并为用户授权、收回权限。

    oracle中如何删除用户? Oracle中使用DROP USER来删除用户,如果使用DROP USER CASCADE那么用户的对象也同时被删除掉.为了达到删除用户的效果而又不影响对用户下的对象的使 ...

  2. MongoDB使用命令创建用户权错误分析--- 权限不够Error:couldn't add user:command createUser requires authentication

    MongoDB使用命令创建用户权错误分析 错误一:权限不够Error:couldn't add user:command createUser requires authentication. 解决方 ...

  3. HBase shell 命令创建表及添加数据操作

    创建表,表名hbase_1102,HBase表是由Key-Value组成的,此表中Key为NAME   此表有两个列族,CF1和CF2,其中CF1和CF2下分别有两个列name和gender,Chin ...

  4. Hive创建表|数据的导入|数据导出的几种方式

    * Hive创建表的三种方式 1.使用create命令创建一个新表 例如:create table if not exists db_web_data.track_log(字段) partitione ...

  5. MongoDB 基础命令——数据库表的增删改查——遍历操作表中的记录

    分组排序查询最大记录 //对 "catagory" 不等于 null 的数据进行分组查询,且查询结果倒序 db.getCollection('userAccount').aggre ...

  6. mysql 如何用命令清除表数据,让表数据索引是从0开始呢?

    truncate MYTABLE 这样就可以了 其实这个命令就相当于删除表再建 所有的数据都还原 可以使用工具来完成这个操作 右键单击要操作的表,选择Turncale Table 执行查询语句,数据就 ...

  7. 9.MongoDB系列之创建副本集(二)

    1. 如何设计副本集 大多数:选取主节点时需要由大多数决定,主节点只有在得到大多数支持时才能继续作为主节点,写操作被复制到大多数成员时就是安全的写操作.这里的大多数定义为"副本集中一半以上的 ...

  8. MongoDB入门命令

    查看所有数据库 > show dbs admin (empty) local 0.078GB > admin和管理相关, admin和local数据库不要轻易动 选择库 > use ...

  9. Jqgrid入门-操作表格的数据(二)

    上一篇中,Jqgrid已经可以从服务端获得数据,并显示在Grid表格中了.下面说一下,如何操作表格及其数据.           jqGrid有很多方法函数,用来操作数据或者操作Grid表格本身.jq ...

随机推荐

  1. 解决跨域、同源策略-React中代理的配置

    React中代理的配置 主要是解决同源策略的问题 何为同源策略? 因为我们React在3000端口,Vue在8080端口,而后台接口往往在5000,这种不同的端口之间就是一种跨域的问题了 axios发 ...

  2. HTML 网页开发、CSS 基础语法——二.互联网原理

    1. 互联网的运行过程 ①用户通过输入网址,发送一个HTTP请求到服务器中去,服务器里面存储了程序员上传的所有网页文件. ② 服务器一旦接收到请求,就会将我们所有的相关网页文件,回传到客户端,通过HT ...

  3. 鸿蒙内核源码分析(源码注释篇) | 鸿蒙必定成功,也必然成功 | 百篇博客分析OpenHarmony源码 | v13.02

    百篇博客系列篇.本篇为: v13.xx 鸿蒙内核源码分析(源码注释篇) | 鸿蒙必定成功,也必然成功 | 51.c.h .o 几点说明 kernel_liteos_a_note | 中文注解鸿蒙内核 ...

  4. P7599-[APIO2021]雨林跳跃【二分,倍增,ST表】

    正题 题目链接:https://www.luogu.com.cn/problem/P7599 题目大意 \(n\)棵树,在某棵树上时可以选择向左右两边第一棵比它高的树跳,现在\(q\)次询问从\([A ...

  5. MFC获取文件路径和文件夹路径

    MFC的界面中,需要实现这样两个功能: 1.在界面上加一个按钮,单击按钮弹出一个对话框选择文件,在工程中获得文件的路径: 2.在界面上加一个按钮,单击按钮弹出一个对话框选择文件夹,在工程中获取文件夹的 ...

  6. 【vue】两个页面间传参 - props

    目录 Step1 设置可以 props 传递数据 Step2 跳转前页面中传递数据 Step3 跳转后的页面接收数据 从 A 页面跳转到 B 页面, 参数/数据通过 props 传递到 B 页面,这种 ...

  7. mac上通过git推送时忽略node_modules文件夹

    node_modules出现改动一般不需要通过git推送,如果不忽略node_modules每次安装新的包会出现几千条新变动,并没有必要推送 1.通过终端进入项目根目录 创建 .gitignore 文 ...

  8. Python代码阅读(第12篇):初始化二维数组

    Python 代码阅读合集介绍:为什么不推荐Python初学者直接看项目源码 本篇阅读的代码实现了二维数组的初始化功能,根据给定的宽高初始化二维数组. 本篇阅读的代码片段来自于30-seconds-o ...

  9. 深入剖析 Spring WebFlux

    一.WebFlux 简介 WebFlux 是 Spring Framework5.0 中引入的一种新的反应式Web框架.通过Reactor项目实现Reactive Streams规范,完全异步和非阻塞 ...

  10. 关于dp那些事

    拿到一道题,先写出状态转移方程,再优化时间复杂度 状态优化: 对于状态可累加 \(e.g.dp[i+j]=dp[i]+dp[j]+i+j\) 的,用倍增优化 决策优化: \(e.g.dp[i][j]= ...