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. 这个 MySQL bug 让我大开眼界

    这周收到一个 sentry 报警,如下 SQL 查询超时了. select * from order_info where uid = 5837661 order by id asc limit 1 ...

  2. 看完小白也会使用,Android投屏神器scrcpy详细教程

    楔子 做为一个软件测试工程师,在使用手机测试的时候,缺陷附件想附上截图.视频,需要从手机把图片.视频发送到拷贝或发送到电脑,非常麻烦. 所以想到使用投屏软件,把手机的屏幕投屏到电脑,便可以直接在电脑上 ...

  3. 『Python』优雅的记录日志——loguru

    1. 安装 pip install loguru 2. 初识 from loguru import logger logger.debug("This is a debug..." ...

  4. Python - with 语句

    管理外部资源的背景 在编程中会面临的一个常见问题是如何正确管理外部资源,例如文件.锁和网络连接 有时,程序会永远保留这些资源,即使不再需要它们,这种现象称为内存泄漏 因为每次创建和打开给定资源的新实例 ...

  5. python二级 之 第 五套

    1. 这里要注意输入的   就是列表 .                 [1,2,3] 2. 就是你要明白   random.seed()  产生随机种子# 与random.randint()  取 ...

  6. P4428-[BJOI2018]二进制【树状数组,set】

    正题 题目链接:https://www.luogu.com.cn/problem/P4428 题目大意 长度为\(n\)的\(0/1\)串要求支持 修改一个位置 求区间\([l,r]\)有多少个子区间 ...

  7. 记一次centos挂载ceph存储的坑

    起因 生产有两台服务器,准备用来跑工作流,执行的资源的是放在ceph存储集群中,第一步挂载ceph 执行命令:mount -t ceph xxx:xxx -o name=admin,secret=AQ ...

  8. @ConfigurationProperties与@Value的区别

    1. 首先我们来看每一个的具体使用 1. ConfigurationProperties 1. application.yml文件中: person: lastName: xj age: 18 bos ...

  9. Sentry 监控 - Distributed Tracing 分布式跟踪

    系列 1 分钟快速使用 Docker 上手最新版 Sentry-CLI - 创建版本 快速使用 Docker 上手 Sentry-CLI - 30 秒上手 Source Maps Sentry For ...

  10. 理解hashMap

    首先需要理解几个基本概念: 什么是数据结构?(摘自 java数据结构系列--什么是数据结构 (baidu.com)) 数据结构是计算机组织.存储数据的方式.简单来说就是,数据按指定的规则进行存储,从而 ...