| 数据集 |
二维表 |
集合 |
| 单条数据 |
行 |
文档 / JSON / BSON |
| 数据含义 |
列 |
键值对 |
| 唯一标识符 |
primary key |
_id(自动生成) |
| 查看数据库列表 |
show databases |
show dbs |
| 选择数据库 |
use <name> |
use <name> |
| 查看数据集 |
show tables |
show collections |
| 查找数据 |
select * from table where row=ele |
db.collection.find( { “row”:“ele”} ) |
| 统计数量 |
select count(*) from table where row=ele |
db.collection.find( { “row”:“ele”}.count() |
| 更友好的查看数据 |
|
db.collection.find( { “row”:“ele”}.pretty() |
| 随机获得一条数据 |
|
db.collection.findOne() |
| 插入 |
insert into table values id=ele; |
db.collection.insert({‘id’:ele}) |
| 插入多条 |
多条insert into |
db.collection.insert( { : } , { : } ) (这是按照顺序插入的,当出现插入错误后直接停止插入) |
| 插入多条(无序) |
|
db.collection.insert( [ { : } , { : } ], { “ordered” : false } ) (出现插入错误后,会从下一个继续进行插入 (相当于错误处理??)) |
| 创建数据库 |
create database name |
use db; (db.dbname.insert({ : }) or db.createCollection(name, option)) |
| 创建数据集 |
create table |
db.collection.insert({ : }) / db.createCollection(name, option) |
| 更新数据 |
|
db.collection.updateMany({“name”:"_name"}, {"$inc":{ : , : }})(增加字段(数字类型)) |
| 更新数据 |
update from table set name=_name where name=_name; |
db.collection.updateMany({“name”:"_name"}, {"$set":{ “name”:"_name", : }})(修改和添加字段) |
| 更新数据 |
|
db.collection.updateMany({查询条件}, {"$push":{ 数组名: { : , :}}})(向数组内添加) |
| 删除数据 |
delete from table |
db.collection.deleteMany() |
| 删除数据 |
delete from table where id=id |
db.collection.deleteMany({id:id}) |
| 删除数据 |
delete from table where id=id |
db.collection.deleteOne({ : }) |
| 删除数据集 |
drop table name |
db.collection.drop() |
| |
可以存在无表的数据库 |
不可以存在无集合的数据库 |