Mongodb安装与使用

简介:

MongoDB 是一个基于分布式文件存储的数据库。由C++语言编写。旨在为WEB应用提供可扩展的高性能数据存储解决方案。

特点:

1高性能

2易部署

3易使用

4存储数据非常方便

安装

下载

: http://www.mongodb.com/download-center?jmp=nav#community

创建数据目录 d:\data\db\

启动

1:在 mongodb 安装目录的 bin 目录下运行 cmd 执行

2:mongod –-dbpath d:\data\db  (启动服务)

3:重新在当前目录下 再次打开一个 cmd 执行 mongo 命令 进入到 mongo 交互界面

【**** 将安装目录的 bin 目录追到的系统path变量里面,之后,任一位置打开cmd都能执行上面步骤了

**** 将mongodb服务写入到系统服务里面,设置自动启动,下次就不需要额外开启服务了。操作如下:

1. 在 d:\data\ 目录下新建一个 logs 文件夹

2. 用管理员权限打开cmd 运行如下命令

mongod –-dbpath d:\data\db --logpath=d:\data\logs\mongodb.log --install】

操作使用

1:初步了解数据库、集合、文档概念

数据库:即存放集合的位置    一个MongoDB中可以建立多个数据库

集合:就是 MongoDB 中的文档组,相当于关系型数据库中的表格

文档:文档就是一个 键值对 对象。一个集合里面可以有多个文档。文档与文档之间不需要设置相同的字段,并且相同的字段也不需要相同的数据类型。这与关系型数据库有很大的区别,也是 MongoDB 非常突出的特点。

2:常用命令

①Help查看帮助命令 

    help        db.help()           db.test.help()                    db.test.find().help()

②创建|切换数据库      

     use 数据库名(例:use project; 若无该project数据库名,则会帮你自动添加并使用)

③列出所有数据库

     show dbs;

④查看当前所处数据库

     db || db.getName()

⑤显示当前db的状态

     db.stats()

⑥查看当前DB的链接机器地址

     db.getMongo()

⑦删除数据库

     db.dropDatabase();

3:MongoDB – Collection 集合操作

 ①创建集合

      db.createCollection(“名字”, {“size”: 20, “capped”: true});

      db.createCollection(“名字”);

      db.”名字”.insertOne({“name”: “bbb”, “age”: 18});

②得到指定名称的集合

      db.getCollection(“account”);

③得到当前db的所有集合

      db.getCollectionNames();

④显示当前db所有集合的状态

      db.printCollectionStats();

⑤删除某个集合

      db.xxx.drop();

4:MongoDB – 增删改 集合数据(重要)

①增加数据

     db.users.save({“name”: “张三”,“age”: 19});

     db.users.insertOne({“name”: “张三”, “age”: 18});

     db.users.insert([{“name”: “zhangsan”}, {“name”: “lisi”}]);

②修改

     db.users.update({“age”: 25}, {$set: {“name”: “zhangsan”}});

     db.users.update({“name”: “lisi”}, {$inc: {“age”: 25}});

     db.users.update({“name”: “lisi”}, {$inc: {“age”: 5},$set: {“name”: “zhangsan”}})

③删除

     db.users.remove({“age”: 25});

④查询

   1、查询所有记录

        db.userInfo.find();

        相当于:select* from userInfo;

    2、查询去重后数据

        db.userInfo.distinct("name");

        相当于:select distict name from userInfo;

    3、查询age = 22的记录

         db.userInfo.find({"age": 22});

         相当于: select * from userInfo where age = 22;

    4、查询age > 22的记录

         db.userInfo.find({age: {$gt: 22}});

         相当于:select * from userInfo where age >22;

     5、查询age < 22的记录

          db.userInfo.find({age: {$lt: 22}});

          相当于:select * from userInfo where age <22;

     6、查询age >= 25的记录

           db.userInfo.find({age: {$gte: 25}});

           相当于:select * from userInfo where age >= 25;

     7、查询age <= 25的记录

            db.userInfo.find({age: {$lte: 25}});

     8、查询age >= 23 并且 age <= 26

            db.userInfo.find({age: {$gte: 23, $lte: 26}});

      9、查询name中包含 mongo的数据

             db.userInfo.find({name: /mongo/});

             //相当于%%

             select * from userInfo where name like ‘%mongo%’;

     10、查询name中以mongo开头的

           db.userInfo.find({name: /^mongo/});

           select * from userInfo where name like ‘mongo%’;

      11、查询指定列name、age数据

           db.userInfo.find({}, {name: 1, age: 1});

           相当于:select name, age from userInfo;

      12、查询指定列name、age数据, age > 25

            db.userInfo.find({age: {$gt: 25}}, {name: 1, age: 1});

            相当于:select name, age from userInfo where age >25;

       13、按照年龄排序

             升序:db.userInfo.find().sort({age: 1});

             降序:db.userInfo.find().sort({age: -1});

        14、查询name = zhangsan, age = 22的数据

            db.userInfo.find({name: 'zhangsan', age: 22});

            相当于:select * from userInfo where name = ‘zhangsan' and age = ’22';

         15、查询前5条数据

           db.userInfo.find().limit(5);

           相当于:select top 5 * from userInfo;

如果这篇文章确实对你有所帮助,欢迎大家点赞,鼓励我继续写下去。有什么不懂的可以问我哦

你可能感兴趣的:(Mongodb安装与使用)