一、数据库常用命令
1、help查看命令提示
help
db.help()
db.test.help()
db.test.find().help()
2、创建、切换数据库
use movies
3、查询数据库
show dbs
4、显示当前DB状态
db.stats()
5、查看当前DB版本
db.version()
6、查看当前DB的链接机器地址
db.getMongo()
7、删除数据库
db.dropDatabase()
8、查询当前所使用的数据库
db.getName()
二、collection聚集集合操作
1、创建一个聚集集合
//collName 相当于表的名称
db.createCollection("collName", {size: 20, capped: true, max: 100});
db.collName.isCapped(); //判断集合是否为定容量

mySQL: INSERT INTO table_name ( field1 ) VALUES ( value1 );
2、得到指定名称的聚集集合
db.getCollection("collName");
3、得到当前DB的所有的聚集集合
db.getCollectionNames();
4、显示当前db所有集合的状态
db.printCollectionStats();

三、document文档操作
1、插入文档
db.users.save({name: 'zhangsan', age: 25, sex: 1});
db.users.insert({name: 'zhangsan', age: 25, sex: 1});
插入单条数据 db.users.insertOne({});
插入多条数据db.users.insertMany([{},{}])

2、更新文档
db.users.updaeMany({},{$set:{class:"1638"}})

//false表示如果不存在不会插入,true表示如果有多个内容都更新(如果为false,则只更新第一条)
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的所有文档
查询所有记录
db.userInfo.find();
相当于:select* from userInfo;
查询去重后数据
db.userInfo.distinct("name");
相当于:select distict name from userInfo;
查询age = 22的记录
db.userInfo.find({"age": 22});
相当于: select * from userInfo where age = 22;
查询age > 22的记录
db.userInfo.find({age: {$gt: 22}});
相当于:select * from userInfo where age >22;
查询age < 22的记录
db.userInfo.find({age: {$lt: 22}});
相当于:select * from userInfo where age <22;

查询age >= 25的记录
db.userInfo.find({age: {$gte: 25}});
相当于:select * from userInfo where age >= 25;
查询age <= 25的记录
db.userInfo.find({age: {$lte: 25}});
查询age >= 23 并且 age <= 26
db.userInfo.find({age: {$gte: 23, $lte: 26}});
查询name中包含 mongo的数据
db.userInfo.find({name: /mongo/});
//相当于%%
select * from userInfo where name like ‘%mongo%’;
查询name中以mongo开头的
db.userInfo.find({name: /^mongo/});
select * from userInfo where name like ‘mongo%’;

4、删除文档
db.users.remove({age: 132},true);
删除多条
db.users.deleteMany({class:"1638"})
删除所有
db.users.remove({})
5、简单查询文档
查询指定列name、age数据
db.userInfo.find({}, {name: 1, age: 1});
相当于:select name, age from userInfo;
查询指定列name、age数据, age > 25
db.userInfo.find({age: {$gt: 25}}, {name: 1, age: 1});
相当于:select name, age from userInfo where age >25;
按照年龄排序
升序:db.userInfo.find().sort({age: 1});
降序:db.userInfo.find().sort({age: -1});
查询name = zhangsan, age = 22的数据
db.userInfo.find({name: 'zhangsan', age: 22});
相当于:select * from userInfo where name = ‘zhangsan' and age = ’22';
查询前5条数据
db.userInfo.find().limit(5);
相当于:select top 5 * from userInfo;
查询10条以后的数据
db.userInfo.find().skip(10);
相当于:select * from userInfo where id not in (
select top 10 * from userInfo
);
查询在5-10之间的数据
db.userInfo.find().limit(5).skip(5);
or与 查询
db.userInfo.find({$or: [{age: 22}, {age: 25}]});
相当于:select * from userInfo where age = 22 or age = 25;
查询第一条数据
db.userInfo.findOne();
相当于:selecttop 1 * from userInfo;
db.userInfo.find().limit(1);
查询某个结果集的记录条数
db.userInfo.find({age: {$gte: 25}}).count();
相当于:select count(*) from userInfo where age >= 20;
按照某列进行排序计数
db.userInfo.find({sex: {$exists: true}}).count();
相当于:select count(sex) from userInfo;

mongodb常用基本命令的更多相关文章

  1. mongodb常用基本命令(根据工作需要,不断更新)

    推荐可视化工具:mongobooster   复制库     db.copyDatabase("ability_message","ability_message_cop ...

  2. Linux(centos)的常用基本命令

    Linux的常用基本命令. 首先启动Linux.启动完毕后需要进行用户的登录,选择登陆的用户不同自然权限也不一样,其中“系统管理员”拥有最高权限. 在启动Linux后屏幕出现如下界面显示: …… Re ...

  3. ffmpeg常用基本命令(转)

    [FFmpeg]FFmpeg常用基本命令 1.分离视频音频流 ffmpeg -i input_file -vcodec copy -an output_file_video //分离视频流 ffmpe ...

  4. Linux的常用基本命令

    Linux的常用基本命令. 首先启动Linux.启动完毕后需要进行用户的登录,选择登陆的用户不同自然权限也不一样,其中“系统管理员”拥有最高权限. 在启动Linux后屏幕出现如下界面显示: …… Re ...

  5. (转)mongodb常用命令脚本化-自动化运维

    mongodb常用命令脚本化-自动化运维 把一些运维中常用到的mongodb命令写成shell脚本,极大的方便了维护   1 设置副本集   #!/bin/bash#mongodb 进入client ...

  6. FFmpeg常用基本命令

    FFmpeg常用基本命令 1.分离视频音频流 ffmpeg -i input_file -vcodec copy -an output_file_video //分离视频流 ffmpeg -i inp ...

  7. mongodb常用操作语句

    mongodb常用操作语句 A:创建数据表 db.createCollection(name, {capped: <Boolean>, autoIndexId: <Boolean&g ...

  8. Linux的常用基本命令。

    Linux的常用基本命令. 首先启动Linux.启动完毕后需要进行用户的登录,选择登陆的用户不同自然权限也不一样,其中"系统管理员"拥有最高权限. 在启动Linux后屏幕出现如下界 ...

  9. mongodb 常用的命令

    mongodb 常用的命令 对数据库的操作,以及登录 1 进入数据库 use admin 2 增加或修改密码 db.addUser('wsc', '123') 3查看用户列表 db.system.us ...

随机推荐

  1. spark算子之Aggregate

    Aggregate函数 一.源码定义 /** * Aggregate the elements of each partition, and then the results for all the ...

  2. 6380. 【NOIP2019模拟2019.10.06】小w与最长路(path)

    题目 题目大意 给你一棵树,对于每一条边,求删去这条边之后,再用一条边(自己定)连接两个连通块,形成的树的直径最小是多少. 正解 首先,将这棵树的直径给找出来.显然,如果删去的边不在直径上,那么答案就 ...

  3. SQL LIKE 运算符

    SQL LIKE 运算符 在WHERE子句中使用LIKE运算符来搜索列中的指定模式. SQL LIKE 操作符 LIKE 操作符用于在 WHERE 子句中搜索列中的指定模式. 有两个通配符与LIKE运 ...

  4. 使用Vue和djangoframwork完成登录页面构建 001

    使用Vue和djangoframwork完成登录页面构建 001 环境的搭建 首先,我在我的电脑的F盘创建了一个文件夹 forNote,进入到这个文件夹中 F:\forNote> vue环境的搭 ...

  5. java 后台 实现简单的验证码

    private int width =80;private int height=30;private Random r=new Random();private String fontnames[] ...

  6. wmic命令用法小例

    wmic就是wmic.exe,位于windows目录底下,是一个命令行程序.WMIC可以以两种模式执行:交互模式(Interactive mode)和非交互模式(Non-Interactive mod ...

  7. error LNK2001: unresolved external symbol "public: __thiscall CWinAppEx::CWinAppEx(int)" (??0CWinApp

    1.这个是网上找的别人的解决办法,本人没试过 解决办法: 网上大多说的这类似问题是解决自定义类的,可是这个连接是系统CWinAppEx,网上查了很久也没找到原因,重装了vs也没解决.后面在google ...

  8. Openstack组件实现原理 — Nova 体系结构

    目录 目录 前文列表 Nova体系结构 虚拟机实例化流程 前文列表 Openstack组件部署 - Overview和前期环境准备 Openstack组建部署 - Environment of Con ...

  9. linux进阶之路(一):linux入门

    Linux:开源.免费得开源系统.具有高效性.稳定性.安全性.处理多并发. Linux的发行版本:基于Linux,不同的安装软件 CentOS(RedHat开源版本) RedHat Ubuntu Su ...

  10. jQuery validate验证隐藏表单(hidden)域

    validate很不错的一个jQuery表单验证插件.升级到了1.9版的后,发现隐藏表单域验证全部失效,特别是在jquery.ui.tabs.min.js构造的Tabs里的验证!网上一搜,也没查到是怎 ...