ITPub博客

首页 > 数据库 > > Mongodb入门 安装 操作指令

Mongodb入门 安装 操作指令

作者:浪花小雨滴 时间:2020-05-12 11:38:56 0 删除 编辑

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

是世界上目前用的最广泛的nosql数据库

noSql 翻译过来 not only sql 不仅仅是sql 他就是一个非关系型数据库

它的特点:高性能、易部署、易使用,存储数据非常方便。

mongodb的优点

  1. 1/面向文档储存的数据库(BSON格式存储)
  2. 2/具有丰富的查询指令
  3. 3/支持索引
  4. 4/具有分片系统
  5. 5/无模式


mongodb的缺点

  1. 1/占用空间非常大
  2. 2/不支持事务
  3. 3/目前已经不维护32位的系统的了

Windows 系统上安装

一、下载

mongodb.org/downloads




1/把mongodb安装到某一个盘符下(D)
2/创建数据目录 D:data/db文件夹
3/找到mongodb文件夹下的bin文件夹打开mongod.exe
4/双击打开mongo.exe
5/浏览器输入127.0.0.1:27017测试安装成功


Mac 系统上安装

一、安装

在Mac OS上面安装MongoDB,你可以通过编译源代码来安装 ,也可以在Mac OS上使用Homebrew安装。

这里介绍使用Homebrew安装MongoDB。

首先更新Homebrew的package数据库:

$ brew update
$ brew install mongodb

二、启动MongoDB

$ mongod --config /usr/local/etc/mongod.conf

三、使用MongoDB

$ mongo


mongo的基础指令

show dbs 获取你当前所有的数据库
use dataBase_name 创建数据库(没有-创建/存在-使用)
db 指查询你当前的数据库
db.stats() 查询你当前数据库的状态
db.dropDatabase() 删除你当前的数据库
db.help() 查询帮助
db.version() 获取你当前数据库的版本
db.database_name.help() 查询任意数据库的帮助
db.collection_name.find() 查询你当前集合内的信息


Collection聚集集合操作

(1) 创建一个聚集集合

db.createCollection("collName",{size: 20, capped: true, max: 100});

(第一个参数集合的名字,后面参数数据;size代表字节;capped规定固定大小,这个参数加上以后可以帮助顺序管理,true代表设置capped;max最大值,size优先级大于max)

db.collName.isCapped(); //判断集合是否为定容量

(2) 得到指定名称的聚集集合

db.getCollection("account");

(3) 得到当前db的所有聚集集合

db.getCollectionNames();

(4) 显示当前db所有聚集的状态

db.printCollectionStats();

(4) 删除集合

db.collectionname.drop();



(1) 添加

db.users.save({name: ‘zhangsan', age: 25,sex: true});

(2) 修改

所有数据都添加一个artist

db.albums.updateMany({},{$set:{artist:‘哈哈’}})
db.users.update({age: 25}, {$set: {name:'changeName'}}, false, true);
相当于:update users set name = ‘changeName' where age = 25;
  • 1/修改的数据不存在---第一个参数false(不添加)true(添加)
  • 2/数据有重复的---第二个参数true符合条件的数据均修改,false默认修改第一条数据
db.users.update({name: 'Lisi'}, {$inc:{age: 50}}, false, true);
相当于:update users set age = age + 50 where name = ‘Lisi';db.users.update({name: 'Lisi'}, {$inc:{age: 50}, $set: {name: 'hoho'}}, false, true);
相当于:update users set age = age + 50, name = ‘hoho' where name = ‘Lisi';

(3) 删除

db.users.remove({age: 132});


(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/})

(10) 查询name中以mongo开头的

db.userInfo.find({name: /^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;

(16) 查询10条以后的数据

db.userInfo.find().skip(10);
相当于:select * from userInfo where id not in (select top 10 * from userInfo);

(17) 限制数据量/几条数据后

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

(18) or与 查询

db.userInfo.find({$or: [{age: 22}, {age:25}]});
相当于:select * from userInfo where age = 22 or age = 25;

(19) 查询第一条数据

db.userInfo.findOne();
相当于:selecttop 1 * from userInfo;
db.userInfo.find().limit(1);

(20) 查询某个结果集的记录条数

db.userInfo.find({age: {$gte:25}}).count();
相当于:select count(*) from userInfo where age >= 20;


(21) 查询某一项的记录数目

db.userInfo.find({sex: {$exists:true}}).count();
相当于:select count(sex) from userInfo;


nodejs链接mongodb数据库

  • 1/引入mongodb依赖在项目中
  • 2/创建数据库连接服务
mongodb.Server()
  • 3/数据库连接
Var db=mongodb.Db()
  • 4/测试链接
db.open()

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31537575/viewspace-3145560/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: tidyfst行操作
请登录后发表评论 登录
全部评论

注册时间:2018-05-16

  • 博文量
    4259
  • 访问量
    40175