- 数据库的使用
mongod –dbpath c:\mongo
管理数据库:mongo (一定要在新的cmd中输入)
2. 数据库操作命令
查看所有数据库列表 :show dbs
使用数据库、创建数据库: use person
数据库中不能直接插入数据,只能往集合(collections)中插入数据。不需要创建集合,系统发现stu是一个陌生的集合名字,所以就自动创建了集合。
db.stu.insert({“name”:”小麦”});
删除数据库,删除当前所在的数据库: db.dropDatabase();
导入数据库文件:
mongoimport –db person –collection stu –drop –file test.json
-db person 往person 数据库里面导入
–collection stu 往stu 中导入
–drop 把集合清空
–file test.json 导入test.json文件
查找数据,用find。find中没有参数,那么将列出这个集合的所有文档:db.stu .find()
精确匹配:
db.stu.find({“score.shuxue”:70});
多个条件:
db.stu.find({“score.shuxue”:70 , “age”:12})
大于条件:
db.stu.find({“score.yuwen”:{$gt:50}});
或者。寻找所有年龄是9岁,或者11岁的学生
db.student.find({$or:[{“age”:9},{“age”:11}]});
查找完毕之后,打点调用sort,表示升降排序。
db.restaurants.find().sort( { “borough”: 1, “address.zipcode”: 1 } )
修改数据update
db.student.update({“name”:”小明”},{$set:{“age”:16}});
更改所有匹配项目
db.student.update({“sex”:”男”},{$set:{“age”:33}},{multi: true});
完整替换,不出现$set关键字了:
db.student.update({“name”:”小明”},{“name”:”大明”,”age”:16});
删除数据
删除所有匹配的数据:db.stu.remove({“age”:15});
删除一条匹配的数据:db.stu.remove({“age”:15},{justOne:true}});