列出collection
show.collections
查看collection中所有数据
db.collection名称.find()
启动mongo并设置数据存储路径
mongod --dbpath 路径
进入mongo运行界面(mongo默认是没有用户和密码的)
mongo
排序(gui工具是不能实现的):
db.student.find().sort({"score.yuwen":-1})---表示按照语文成绩正序排序
db.student.find().sort({"score.yuwen":-1}).sort({"score.shuxue":-1})---表示先按照数学成绩排名,成绩相同的按照语文成绩排名(次序是由后到前)
修改
db.student.update({"name":"小明"},{$set:{"age":16}})---将姓名为小明的age修改为16
db.student.update({},{$set:{age:30}},{multi:true})-------加multi:true,表示修改许多项数据
db.student.update({"name":"
小明
"},{"name":"
大明
","age":16});-----完整替换,在这里把小明中所有的数据都删除了,直接替换成后面的数据。
删除数据
db.student.remove({"name":"小红"})---删除姓名为小红的数据
区别:
db.student.remove({}):删除所有数据,但不删除文档这个架子
db.student.drop():删除数据和文档这个架子
查询数据
db.student.find({"name":"小红"})----查询姓名为小红的数据
db.student.find({"age":{$gt:10}})----查询年龄大于10的数据
db.student.find({"age":{$gt:10},"score.shuxue":{$gt:70}})---查询年龄大于10并且数学成绩大于70的数据
分页查询
db.teacher.find().limit(5).skip(0);---表示从第一条数据开始,查询5条数据
列出所有条数
db.teacher.find().count();---列出teacher有多少数目的数据
注意:
创建数据库后,必须往这个数据库中集合插入一条数据后,这个数据库才能创建成功。(防止用户输入错误的数据库名,导致创建数据库)