```
开机
mongod --dbpath d:\mongod
开机并设置存储引擎
mongod --storageEngine mmapv1 --dbpath d:\mongo
show dbs 显示所有库
use hello 进入库, 没有就新建一个
db查看当前库
student是一个集合
导入数据
mongoimport --db hello --collection student --file d:\1.json
插入一条数据
db.student.insert({"name":"mongo","insertTime":2017});
精确匹配:
db.student.find({"score.shuxue":70});
多个条件:
db.student.find({"score.shuxue":70 , "age":12})
大于条件:
db.student.find({"score.yuwen":{$lt:50}});
大于条件:
db.student.find({"score.yuwen":{$gt:50}});
或者条件查询
sb.student.find({$or:[{"age":9},{"age":11}]});
或者。寻找所有年龄是9岁,或者11岁的学生
db.student.find({$or:[{"age":9},{"age":11}]});
查找完毕之后,打点调用sort,表示升降排序, 1是升序, -1是倒序
db.restaurants.find().sort( { "borough": 1, "address.zipcode": 1 } )
多条件查找
db.student.find({"age":9},{"score.shuxue":80});
数组查找
db.student.find("hobby":"睡觉");
更改所有匹配项目:"
db.student.update({"sex":"男"},{$set:{"age":33}},{multi: true});
查找名字叫做小明的,把年龄更改为16岁:
db.student.update({"name":"小明"},{$set:{"age":16}});
查找数学成绩是70,把年龄更改为33岁:
db.student.update({"score.shuxue":70},{$set:{"age":33}});
删除一个 justOnt:true
db.student.remove( { "borough": "Queens" }, { justOne: true } )
删除匹配的数据
db.student.remove({"age":9});
删除集合中的所有数据
db.student.drop();
修改数据
删除整个数据库,删除当前所在的数据库
db.dropDatabase();
/* 0 */
{
"_id" : ObjectId("58c68de2cecb89118397a2e2"),
"name" : "小明",
"age" : 12,
"hobby" : ["睡觉", "吃饭"],
"score" : {
"yuwen" : 59,
"shuxue" : 80
}
}
/* 1 */
{
"_id" : ObjectId("58c68de2cecb89118397a2e3"),
"name" : "小红",
"age" : 11,
"hobby" : ["学习", "看书"],
"score" : {
"yuwen" : 100,
"shuxue" : 70
}
}
```