首页 > 数据库 > > Mongodb入门 安装 操作指令
一、下载
http://www.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 OS上面安装MongoDB,你可以通过编译源代码来安装 ,也可以在Mac OS上使用Homebrew安装。
这里介绍使用Homebrew安装MongoDB。
首先更新Homebrew的package数据库:
$ brew update
$ brew install mongodb二、启动MongoDB
$ mongod --config /usr/local/etc/mongod.conf三、使用MongoDB
$ mongoshow dbs 获取你当前所有的数据库
use dataBase_name 创建数据库(没有-创建/存在-使用)
db 指查询你当前的数据库
db.stats() 查询你当前数据库的状态
db.dropDatabase() 删除你当前的数据库
db.help() 查询帮助
db.version() 获取你当前数据库的版本
db.database_name.help() 查询任意数据库的帮助
db.collection_name.find() 查询你当前集合内的信息(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;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;mongodb.Server()Var db=mongodb.Db()db.open()来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31537575/viewspace-3145560/ ,如需转载,请注明出处,否则将追究法律责任。