1.查看数据库
show dbs
2.创建和删除数据库
//创建数据库,名为mydb
use mydb
//删除
db.dropDatabase()
3.创建和删除集合
//进入数据库
use mydb
//创建集合,名为mycoll
db.createCollection("mycoll")
//删除集合
db.mycoll.drop()
4.查看集合
show collections
5.插入数据
db.mycoll.insert({"name":"mongodb"})
//插入嵌套文档
db.mycoll.insert({"name":"xiaoming","age":18,
"tags":["father","son","husband"],
"familly":{"father":"ming","son":"ming","wife":"xiaoxin"}})
//还可以以变量的形式插入数据
document=({"name":"mongo"})
db.mycoll.insert({document)
6.查询集合中的数据find(query)
//查询所有数据
db.mycoll.find()
db.mycoll.find().pretty()
//条件查询
db.mycoll.find(查询条件)
//例如
db.mycoll.find({"name":"mongodb"})
//查询年龄大于17的数据 =($eq) !=($ne) <($lt) <=($lte) >($gt) >=($gte)
db.mycoll.find({"age":{$gt:17}})
7.更新数据update(query,update,upsert,multi)
- query查询条件
- update更新操作
- upsert 可选,不存在则插入,默认是false
- multi 可选,更新多行,默认是false
db.mycoll.update({"name":"mongodb"},{$set{"name":"mongodb命令"}},{upsert:true},{multi:true})
8.删除数据remove(query)
db.mycoll.remove({"name":"mongo"})
9.$type操作符
//查找name为string类型的数据
db.mycoll.find({"name":{$type:2}})