MongoDB常用操作

本文涉及的产品
云数据库 MongoDB,通用型 2核4GB
简介: MongoDB常用操作

MongoDB常用操作
一查询find方法db.collection_name.find()

select * from users;
db.users.find();
指定返回那些列(键):
select name, skills from users;
db.users.find({}, {'name' : 1, 'skills' : 1});
第一个{} 放where条件 第二个{} 指定那些列显示和不显示 (0表示不显示 1表示显示)

where条件:
1.简单的等于:
select name, age, skills from users where name = 'hurry';
db.users.find({'name' : 'hurry'},{'name' : 1, 'age' : 1, 'skills' : 1});

2.使用and
select name, age, skills from users where name = 'hurry' and age = 18;
db.users.find({'name' : 'hurry', 'age' : 18},{'name' : 1, 'age' : 1, 'skills' : 1});

3.使用or
select name, age, skills from users where name = 'hurry' or age = 18;
db.users.find({ '$or' : [{'name' : 'hurry'}, {'age' : 18}] },{'name' : 1, 'age' : 1, 'skills' : 1});

4.<, <=, >, >= ($lt, $lte, $gt, $gte )
$gt:大于
$lt:小于
$gte:大于或等于
$lte:小于或等于
select * from users where age >= 20 and age <= 30;
db.users.find({'age' : {'$gte' : 20, '$lte' : 30}});

5.使用in, not in ($in, $nin)
select * from users where age in (10, 22, 26);
db.users.find({'age' : {'$in' : [10, 22, 26]}});

6.匹配null
select * from users where age is null;
db.users.find({'age' : null);

7.like (mongoDB 支持正则表达式)
select * from users where name like "%hurry%";
db.users.find({name:/hurry/});
select * from users where name like "hurry%";
db.users.find({name:/^hurry/});

8.使用distinct
select distinct (name) from users;
db.users.distinct('name');

9.使用count
select count(*) from users;
db.users.count();

10.排序
其中 1 为升序排列,而 -1 是用于降序排列。
db.documents.find({}).sort({"created_at":-1});

相关实践学习
MongoDB数据库入门
MongoDB数据库入门实验。
快速掌握 MongoDB 数据库
本课程主要讲解MongoDB数据库的基本知识,包括MongoDB数据库的安装、配置、服务的启动、数据的CRUD操作函数使用、MongoDB索引的使用(唯一索引、地理索引、过期索引、全文索引等)、MapReduce操作实现、用户管理、Java对MongoDB的操作支持(基于2.x驱动与3.x驱动的完全讲解)。 通过学习此课程,读者将具备MongoDB数据库的开发能力,并且能够使用MongoDB进行项目开发。 &nbsp; 相关的阿里云产品:云数据库 MongoDB版 云数据库MongoDB版支持ReplicaSet和Sharding两种部署架构,具备安全审计,时间点备份等多项企业能力。在互联网、物联网、游戏、金融等领域被广泛采用。 云数据库MongoDB版(ApsaraDB for MongoDB)完全兼容MongoDB协议,基于飞天分布式系统和高可靠存储引擎,提供多节点高可用架构、弹性扩容、容灾、备份回滚、性能优化等解决方案。 产品详情: https://www.aliyun.com/product/mongodb
相关文章
|
9月前
|
SQL NoSQL 关系型数据库
【mongo 系列】mongodb 学习三,常用操作实际操练
可以使用 insert,insertOne,insertMany 插入不同的数据,各取所需,其中 insertMany 用于插入多条数据,当然也可以插入 1 条数据
105 4
|
SQL 存储 NoSQL
MongoDB从入门到实战之MongoDB工作常用操作命令
MongoDB从入门到实战之MongoDB工作常用操作命令
229 0
|
NoSQL 关系型数据库 数据库
|
NoSQL 数据库 索引
|
NoSQL Shell 数据库
【mongodb系统学习之八】mongodb shell常用操作
<p style="margin-top:5.28pt; margin-bottom:0pt; margin-left:0.38in; text-indent:-0.38in; direction:ltr; unicode-bidi:embed; word-break:normal"> <span style="font-size:22pt; font-family:黑体"><stron
1140 0
|
1天前
|
NoSQL MongoDB 数据库
MongoDB数据恢复—MongoDB数据库文件被破坏的数据恢复案例
服务器数据恢复环境: 一台Windows Server操作系统服务器,服务器上部署MongoDB数据库。 MongoDB数据库故障&检测: 工作人员在未关闭MongoDB数据库服务的情况下,将数据库文件拷贝到其他分区。拷贝完成后将原MongoDB数据库所在分区进行了格式化操作,然后将数据库文件拷回原分区,重新启动MongoDB服务,服务无法启动。