mongodb
下载&安装
启动&退出
启动服务端
mongod
启动客户端
mongo
正常退出
use admin;
db.shutdownServer();
插入
使用test库,在员工表插入2行数据
use test
db.createCollection("employee")
db.employee.insert([{"sid":1001,"sname":"Tom","age":35,"gender":"male","phone":13981234567,"address":[{"city":"Chengdu","district":"jinniu"}]},{"sid":1002,"sname":"Jack","age":26,"gender":"male","phone":13981357913,"address":[{"city":"Chengdu","district":"wuhou"}]}])
查询
MongoDB 查询文档
查询员工表名字为Tom的
db.employee.find({"sname":"Tom"})
查询员工表中年龄大于35岁的
db.employee.find({"age": {$gt:35}}).pretty()
更新
更新员工表名字为Tom的电话号码
db.employee.update({"sname":"Tom"}, {$set:{"phone":"18200753159"}})
更新员工表名字为Tom的住址区域-含数组的更新示例
db.employee.update({"sname":"Tom"}, {$set:{"address.0.district":"chenghua"}})
索引
给sid和sname字段建立升序索引
db.employee.ensureIndex({"sid":1,"sname":1})
查看执行计划,分析是否走了索引
db.employee.find({"sid":1001}).explain()