windows下mongodb安装及基本操作

一、安装
1.下载mondodb;
2.选择自定义安装;

若安装失败:1.之前安装过,残留注册表,==》使用杀毒软件清理注册表。

3.安装成功后,
3.1.配置环境变量:在系统变量中的path中添加。
注:必须是系统变量,若是用户变量可能会出错。
3.2.创建data->db文件夹,指定数据文件的备用路径。

“F:\Program Files\MongoDB\Server\3.6\bin\mongod.exe" --dbpath "F:\Program Files\MongoDB\Server\3.6\data\db"

3.3.在 随便一个目录下新建一个文件夹,用来存放mongo数据库文件。比如,在D盘下新建mongo文件夹。
4.在以后需要开启数据库的时候,先以管理员权限打开cmd,执行:mongod --dbpath D:\\mongo; 然后重开一个cmd,执行:mongo

二、基本操作

清屏:

  cls


查看所有数据库列表

   show dbs


使用数据库、创建数据库

use itcast

删除数据库,删除当前所在的数据库

db.dropDatabase();



插入数据

插入数据,随着数据的插入,数据库创建成功了,集合也创建成功了。

db.student.insert({"name":"xiaoming"});

我们不可能一条一条的insert。所以,我们希望用sublime在外部写好数据库的形式,然后导入数据库:

mongoimport --db test --collection restaurants --drop --file primer-dataset.json

-db test  想往哪个数据库里面导入

--collection restaurants 想往哪个集合中导入

--drop 把集合清空

--file primer-dataset.json 哪个文件

这样,我们就能用sublime创建一个json文件,然后用mongoimport命令导入,这样学习数据库非常方便。

查找数据

查找数据,用find。find中没有参数,那么将列出这个集合的所有文档:

db.restaurants.find()

精确匹配:

db.student.find({"score.shuxue":70});

多个条件:

db.student.find({"score.shuxue":70 , "age":12})

 大于条件:

db.student.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 } )

修改数据

修改里面还有查询条件

查找名字叫做小明的,把年龄更改为16岁:

db.student.update({"name":"小明"},{$set:{"age":16}});

 查找数学成绩是70,把年龄更改为33岁:

db.student.update({"score.shuxue":70},{$set:{"age":33}});

更改所有匹配项目:"

By default, the update() method updates asingle document. To update multiple documents, use the multi option in theupdate() method.

1           db.student.update({"sex":"男"},{$set:{"age":33}},{multi: true});

 完整替换,不出现$set关键字了:

db.student.update({"name":"小明"},{"name":"大明","age":16});

删除数据

db.restaurants.remove( { "borough": "Manhattan" } )





猜你喜欢

转载自blog.csdn.net/sctec/article/details/78934840