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. chrome浏览器中安装以及使用Elasticsearch head 插件

    一.安装Elasticsearch head 插件 下载安装包:https://github.com/liufengji/es-head/commit/121cdcb6d1b18656461e4889 ...

  2. Jmeter监控技术实战

    性能测试中监控的意义 为性能分析提供依据 监控方案 serverAgent jmeter的插件,监控颗粒度不高,界面简陋 服务器中启动 jmeter中添加插件 Nmon Grafana 优秀监控方案所 ...

  3. python三种导入模块的方法

    做为python初学者,有时候搞不清楚导入模块的作用. 直接导入模块 通常模块为一个文件,直接使用import来导入就好了.可以作为module的文件类型有".py"." ...

  4. whistle抓包-数据包分析

    额,这篇忘了是来自哪位作者的了. whistle:1.14.6 这里以抓取浏览器数据包为例,分析抓取的数据. Method:Connect,对应Host:Tunnel to意思是因为网络环境受限,客户 ...

  5. 『PyTorch』屌丝的PyTorch玩法

    1. prefetch_generator 使用 prefetch_generator库 在后台加载下一batch的数据,原本PyTorch默认的DataLoader会创建一些worker线程来预读取 ...

  6. 鸿蒙内核源码分析(汇编基础篇) | CPU在哪里打卡上班? | 百篇博客分析OpenHarmony源码 | v22.01

    百篇博客系列篇.本篇为: v22.xx 鸿蒙内核源码分析(汇编基础篇) | CPU在哪里打卡上班 | 51.c.h .o 硬件架构相关篇为: v22.xx 鸿蒙内核源码分析(汇编基础篇) | CPU在 ...

  7. ARM平台如何玩转GDB远程调试?

    前  言 关于GDB工具 GDB工具是GNU项目调试器,基于命令行使用.和其他的调试器一样,可使用GDB工具单步运行程序.单步执行.跳入/跳出函数.设置断点.查看变量等等,它是UNIX/LINUX操作 ...

  8. USACO Section 4

    前言 好久没更新这个系列了,最近闲的无聊写一下.有两题搜索懒得写了. P2737 [USACO4.1]麦香牛块Beef McNuggets https://www.luogu.com.cn/probl ...

  9. P6847-[CEOI2019]Magic Tree【dp,线段树合并】

    正题 题目链接:https://www.luogu.com.cn/problem/P6847 题目大意 \(n\)个点的一棵树上,每个时刻可以割掉一些边,一些节点上有果实表示如果在\(d_i\)时刻这 ...

  10. react之组建通信

    父组件与子组件通信 父组件将自己的状态传递给子组件,子组件当做属性来接收,当父组件更改自己状态的时候,子组件接收到的属性就会发生改变 父组件利用ref对子组件做标记,通过调用子组件的方法以更改子组件的 ...