mongodb指令和sql语句对比
show dbs
--show databases;
use student //如果不存在test3数据库,则创建数据库test3,所以也用作创建数据库, 但是如果创建数据库test3后没有增加数据,则认为是手误,并不会创建test3数据库。
-- use student
collection //集合,Mongodb中没有表这个说法,只有collection集合,相当于一张表。
--table
db.getName() //获取当前数据库名
db.createCollection('集合名')
--create table 表名
show collections
--show tables
增
db.student.save({name:'panda',age:18,sex:0})
-- insert into student(name,age,sex) values('panda', 18, 0)
查
db.student.find()
-- select * from student
db.student.find({name:'monkey'})
-- select * from student where name = 'monkey'
db.studnet.find({age:{$lt:18}})
--select * from student where age < 18;
db.studnet.find({age:{$lte:18}})
--select * from student where age <= 18;
db.student.distinct('name')
-- select distinct name from student
db.student.find({$and:[age:18,name:'monkey']})
--select * from student where age = 18 and name = 'monkey'
db.student.find({$or:[name:'monkey', age:18]})
-- select * from student where age=18 or name='monkey'
db.student.find({age:{$gt:18,$lt:20}})
--select * from student where age>18 and age<20
db.student.find().limit(2)
--select * from studnet limit 2
db.studnet.find().skip(1).limit(2)
-- select * from student limit 1, 2
db.student.find().count()
--select count(*) from student
删
db.student.remove({sex:0})
--delete from student where sex = 0
改
db.student.update({name:'monkey'}, {$set:{age:19}}, false, true)
第一个参数是查询条件,第二个参数是执行什么操作,$set就代表设定值,第三个false参数是upsert,如果值为false,则如果没有符合查询条件的数据,不会做什么操作,如果值为true,则会增加一条数据。第四个参数为false,表示只对满足查询条件的第一条数据进行更新,若为false,则多所有满足查询条件的数据进行更新。