`

MongoDB于SQL的对应

阅读更多

 

首先介绍一些关系数据库中常用的概念对比MongoDB中与之对应的概念。

 

                                      Oracle                                                                                                          MongoDB

                                     DataBase                                                                                                      DataBase

                                     Table(表)                                                                                                       Collection(集合)

                                    index(索引)                                                                                                      index(索引)

                                     row(一行记录)                                                                                  BSON(类似JSON格式) http://bsonspec.org/

                                      column(列,字段)                                                                                          BSON中的字段

                                      join(连接)                                                                                  embedding and linking(嵌入和连接)

                                     primary key(主键)                                                                                       _id field(ID标识符)

                                      group by(分组)                                                                                        aggregation(聚合)

                                      其实在学习MongoDB过程中我们就是要忘记模式,记住”键值对”就可以啦。

                       MongoDB的查询是通过JSON(BSON)对象,来表示的。接下来我们将通过对比展示SQL和MonggoDB的查询语法

 

                                                                SQL语句                                                                                             MongoDB语句

                Create table users(a int,b int)建立一张表                     我们无需显式创建Collection,前面讲了在我们保存第一条文档的时候MongoDB会自动创建,当然我们也可以显示的建立:
                                                                            Collection: db.createCollection("users", { capped:true, size:100000}) // capped:是否覆盖 size:大小以字节为单位

 

                             Alter table users………                                                                                                                 模式自由

 

inert into users value(3,5)

db.users.insert({a:3,b:5})

 

 

select a,b from users

db.users.find({}, {a:1,b:1})

select * from users

db.users.find()

select * from users where age=33

db.users.find({age:33})

select a,b from users where age=33

db.users.find({age:33}, {a:1,b:1})

select * from users where age=33 order by name

db.users.find({age:33}).sort({name:1})

select * from users where age>33

db.users.find({age:{$gt:33}})

select * from users where age!=33

db.users.find({age:{$ne:33}})

select * from users where name like "%Joe%"

db.users.find({name:/Joe/})

select * from users where name LIKE "Joe%"

db.users.find({name:/^Joe/})

select * from users where age>33 and age<=40

db.users.find({'age':{$gt:33,$lte:40}})

select * from users order by name desc

db.users.find().sort({name:-1})

select * from users where a=1 and b='q'

db.users.find({a:1,b:'q'})

select * from users limit 10 skip 20

db.users.find().limit(10).skip(20)

select * from users where a=1 or b=2

db.users.find( { $or : [ { a : 1 } , { b : 2 } ] } )

select * from users limit 1

db.users.findOne()

select order_id from orders o, order_line_items li where li.order_id=o.order_id and li.sku=12345

db.orders.find({"items.sku":12345},{_id:1})

select customer.name from customers,orders where orders.id="q179" and orders.custid=customer.id

var o = db.orders.findOne({_id:"q179"});

var name = db.customers.findOne({_id:o.custid})

 

 

select distinct last_name from users

db.users.distinct('last_name')

select count(*y)

from users

db.users.count()

select count(*y)

from users where age > 30

db.users.find({age: {'$gt': 30}}).count()

select count(age) from users

db.users.find({age: {'$exists': true}}).count()

 

 

create index myindexname on users(name)

db.users.ensureIndex({name:1})

create index myindexname ON users(name,ts desc)

db.users.ensureIndex({name:1,ts:-1})

 

 

explain select * from users where z=3

db.users.find({z:3}).explain()

 

 

update users set a=1 where b='q'

db.users.update({b:'q'}, {$set:{a:1}}, false, true)

update users set a=a+2 where b='q'

db.users.update({b:'q'}, {$inc:{a:2}}, false, true)

 

 

delete from users where z="abc"

db.users.remove({z:'abc'});

 

更多请见: http://api.mongodb.org/wiki/current/SQL%20to%20Aggregation%20Framework%20Mapping%20Chart.html

这些都是常用的SQL语句,当然掌握这些就足够后面的开发啦。

 

SQL Statement Mongo Query Language Statement
CREATE TABLE USERS (a Number, b Number) Implicit or use MongoDB::createCollection().
INSERT INTO USERS VALUES(1,1) $db->users->insert(array("a" => 1, "b" => 1));
SELECT a,b FROM users $db->users->find(array(), array("a" => 1, "b" => 1));
SELECT * FROM users WHERE age=33 $db->users->find(array("age" => 33));
SELECT a,b FROM users WHERE age=33 $db->users->find(array("age" => 33), array("a" => 1, "b" => 1));
SELECT a,b FROM users WHERE age=33 ORDER BY name $db->users->find(array("age" => 33), array("a" => 1, "b" => 1))->sort(array("name" => 1));
SELECT * FROM users WHERE age>33 $db->users->find(array("age" => array('$gt' => 33)));
SELECT * FROM users WHERE age<33 $db->users->find(array("age" => array('$lt' => 33)));
SELECT * FROM users WHERE name LIKE "%Joe%" $db->users->find(array("name" => new MongoRegex("/Joe/")));
SELECT * FROM users WHERE name LIKE "Joe%" $db->users->find(array("name" => new MongoRegex("/^Joe/")));
SELECT * FROM users WHERE age>33 AND age<=40 $db->users->find(array("age" => array('$gt' => 33, '$lte' => 40)));
SELECT * FROM users ORDER BY name DESC $db->users->find()->sort(array("name" => -1));
CREATE INDEX myindexname ON users(name) $db->users->ensureIndex(array("name" => 1));
CREATE INDEX myindexname ON users(name,ts DESC) $db->users->ensureIndex(array("name" => 1, "ts" => -1));
SELECT * FROM users WHERE a=1 and b='q' $db->users->find(array("a" => 1, "b" => "q"));
SELECT * FROM users LIMIT 10 SKIP 20 $db->users->find()->limit(10)->skip(20);
SELECT * FROM users WHERE a=1 or b=2 $db->users->find(array('$or' => array(array("a" => 1), array("b" => 2))));
SELECT * FROM users LIMIT 1 $db->users->find()->limit(1);
EXPLAIN SELECT * FROM users WHERE z=3 $db->users->find(array("z" => 3))->explain()
SELECT DISTINCT last_name FROM users $db->command(array("distinct" => "users", "key" => "last_name"));
SELECT COUNT(*y) FROM users $db->users->count();
SELECT COUNT(*y) FROM users where AGE > 30 $db->users->find(array("age" => array('$gt' => 30)))->count();
SELECT COUNT(AGE) from users $db->users->find(array("age" => array('$exists' => true)))->count();
UPDATE users SET a=1 WHERE b='q' $db->users->update(array("b" => "q"), array('$set' => array("a" => 1)));
UPDATE users SET a=a+2 WHERE b='q' $db->users->update(array("b" => "q"), array('$inc' => array("a" => 2)));
DELETE FROM users WHERE z="abc" $db->users->remove(array("z" => "abc"));
分享到:
评论

相关推荐

    MongoDB常用SQL操作.pdf

    MongoDB常用SQL操作,包括dml 和ddl语句,每条sql语句都有对应的示例

    sql mongo对应写法

    sql mongodb对应写法 方便大家查询

    No Sql Manager for mongodb 3.7

    1. 对于MongoDB的3.1完全支持 2. 对认证企业的MongoDB 2.4和2.6版本 3. 全功能蒙戈GUI外壳采用代码自动完成和提示 4. 支持副本集和独立主机的连接 5. 易于使用的文档查看器和编辑器树,表和JSON视图模式 6. 数据库,...

    windows服务自动定时启动SQLServer同步数据到MongoDB.zip(c#源代码)

    本软件使用c#编写,是SQL转存MongoDB的工具,可独立运行,也可定时运行,利用sql数据库时间戳字段进行更新采集区分。 本软件综合了,windows服务控制(安装卸载等),windows服务启动程序(服务控制定时运行程序),...

    MongoDB 中聚合统计计算–$SUM表达式

    我们一般通过表达式$sum来计算总和。因为MongoDB的文档有数组字段,所以可以简单的将计算总和分成两种: 1,统计符合条件的所有文档的某个字段的总和;... description: 'MongoDB is no sql database', by_user

    MongoDB与MySQL的操作对比表及区别介绍

    所以我们所熟知的那些SQL(全称Structured Query Language)语句就不适用于MongoDB了,因为SQL语句是关系型数据库的标准语言。   以我们公司项目为例,在早期的项目中,都在使用关系型数据库,用过SQLServer,Oracle...

    mongodb与sql关系型数据比较

    摸索了几天,大体也初步算入了mongodb的门,仔细一想,mongodb和传统关系型数据库差别很大了。...那如果是在mongodb中,那就对应一个文件了 举个例子就是这样的数据了 { Name:'小明',Sex:'男',Age:'25',

    MongoDB应用.doc

    MongoDB是一个面向文档的数据库系统。使用C++编写,不支持SQL,但有自己功能强大的查询语法。 MongoDB使用BSON作为数据存储和传输的格式。...MongoDB很像MySQL,document对应MySQL的row,collection对应MySQL的table。

    SQL to Mongo Mapping Chart

    SQL to Mongo Mapping Chart,SQL与mongo 查询对应关系

    MongoDB入门

    MongoDB很像MySQL,document对应MySQL的row,collection对应MySQL的table。MongoDB在Windows上的安装运行很方便。直接下载、解压,然后运行bin/mongod即可启动服务器,运行bin/mongo即可运行命令行客户端。官方网站...

    MongoDB与MySQL常用操作语句对照

    一、MongoDB对MySQL常用的SQL语句对应的实现 代码如下: —————————————— MySQL: SELECT * FROM user Mongo: db.user.find() —————————————— MySQl: SELECT * FROM user WHERE ...

    基于SpringBoot快速开发的爬虫项目源码+项目使用说明+sql数据库.zip

    【资源说明】 基于SpringBoot快速开发的爬虫项目源码+项目使用说明+sql数据库.zip 基于SpringBoot快速开发的爬虫项目源码+项目...2. 启动项目即可自动爬取相关数据到mongoDB对应的集合中(集合名称为{spider_name})

    dbeaver对应的各类数据库驱动

    dbeaver连接数据库时,需要下载连接数据库相应驱动程序,...但如果是公司内网或没有外网情况下,需要离线安装,本资源提供各主流数据库对应的离线驱动包,包括mysql(5.0和8.0)、oracle、pgsql、sql server、sqlite等

    Node.js对MongoDB进行增删改查操作的实例代码

    NoSQL数据库中的文档(documents)对应于SQL数据库中的一行。将一组文档组合在一起称为集合(collections),它大致相当于关系数据库中的表。 除了作为一个NoSQL数据库,MongoDB还有一些自己的特性: •易于安装和设置 ...

    java操作geoserver的工具类GeoServer,配合geoserver-manager包使用

    java操作geoserver的工具类,包含mongodb和sqlserver创建数据存储的对象, 封装好了图层的shp发布,数据库空间表发布,图层列表查询,删除,发布样式,设置图层默认样式,发布带样式的空间表,删除样式,查询图层中心...

    SimpleCloud:其他Minecraft云系统的简单替代

    MongoDB和SQL支持 仪表盘 要求 Java 8 有效的MongoDB或SQL数据库安装 2GB记忆体 2个虚拟核心 设置 从下载云。 解压缩文件夹并执行开始文件 请遵循安装说明 将包装器连接到您的经理。 建议为此使用...

    c#操作mongodb插入数据效率

    mongodb的数据插入速度是其一个亮点,同样的10000条数据,插入的速度要比Mysql和sqlserver都要快,当然这也是要看使用者怎么个使用法,你代码如果10000次写入使用10000次连接,那也是比不过其他数据库使用事务一次性...

    c# 插入数据效率测试(mongodb)

    mongodb的数据插入速度是其一个亮点,同样的10000条数据,插入的速度要比Mysql和sqlserver都要快,当然这也是要看使用者怎么个使用法,你代码如果10000次写入使用10000次连接,那也是比不过其他数据库使用事务一次性...

    flink1.14.3 cdc jar包

    flink-1.14.3 的所有flink cdc jar包,包含 oracle,mysql,postgres,sqlserver,mongodb

    sqltoy-orm框架系统是比hibernate+myBatis更加贴合项目的orm框架.rar

    1 sqltoy-orm是什么 sqltoy-orm是比hibernate+myBatis(plus)更加贴合项目的orm框架(依赖spring),具有jpa式的对象CRUD的同时具有比myBatis(plus)更直观简洁性能强大的查询...通过quickvo工具从数据库生成对应的POJO,

Global site tag (gtag.js) - Google Analytics