Sql与Mongodb 查询映射图
来源:互联网 发布:出试卷软件 编辑:程序博客网 时间:2024/05/01 21:08
SQL to MongoDB Mapping Chart
In addition to the charts that follow, you might want to consider theFrequently Asked Questions section for a selection of common questions about MongoDB.
Executables
The following table presents the MySQL/Oracle executables and thecorresponding MongoDB executables.
Terminology and Concepts
The following table presents the various SQL terminology and conceptsand the corresponding MongoDB terminology and concepts.
primary key
Specify any unique column or column combination as primarykey.
primary key
In MongoDB, the primary key is automatically set to the_id field.
aggregation (e.g. group by)aggregation framework
See the SQL to Aggregation Framework Mapping Chart.
Examples
The following table presents the various SQL statements and thecorresponding MongoDB statements. The examples in the table assume thefollowing conditions:
The SQL examples assume a table named users.
The MongoDB examples assume a collection named users that containdocuments of the following prototype:
{ _id: ObjectID("509a8fb2f3f4948bd2f983a0"), user_id: "abc123", age: 55, status: 'A'}
Create and Alter
The following table presents the various SQL statements related totable-level actions and the corresponding MongoDB statements.
CREATE TABLE users ( id MEDIUMINT NOT NULL AUTO_INCREMENT, user_id Varchar(30), age Number, status char(1), PRIMARY KEY (id))
Implicitly created on first insert operation. The primary key_idis automatically added if_id field is not specified.
db.users.insert( { user_id: "abc123", age: 55, status: "A" } )
However, you can also explicitly create a collection:
db.createCollection("users")
ALTER TABLE usersADD join_date DATETIME
Collections do not describe or enforce the structure of itsdocuments; i.e. there is no structural alteration at thecollection level.
However, at the document level, update() operations can add fields to existingdocuments using the$set operator.
db.users.update( { }, { $set: { join_date: new Date() } }, { multi: true })
ALTER TABLE usersDROP COLUMN join_date
Collections do not describe or enforce the structure of itsdocuments; i.e. there is no structural alteration at the collectionlevel.
However, at the document level, update() operations can remove fields fromdocuments using the$unset operator.
db.users.update( { }, { $unset: { join_date: "" } }, { multi: true })
CREATE INDEX idx_user_id_ascON users(user_id)
db.users.ensureIndex( { user_id: 1 } )
CREATE INDEX idx_user_id_asc_age_descON users(user_id, age DESC)
db.users.ensureIndex( { user_id: 1, age: -1 } )
DROP TABLE users
db.users.drop()
Insert
The following table presents the various SQL statements related toinserting records into tables and the corresponding MongoDB statements.
INSERT INTO users(user_id, age, status)VALUES ("bcd001", 45, "A")
db.users.insert( { user_id: "bcd001", age: 45, status: "A"} )
Select
The following table presents the various SQL statements related toreading records from tables and the corresponding MongoDB statements.
SELECT *FROM users
db.users.find()
SELECT id, user_id, statusFROM users
db.users.find( { }, { user_id: 1, status: 1 })
SELECT user_id, statusFROM users
db.users.find( { }, { user_id: 1, status: 1, _id: 0 })
SELECT *FROM usersWHERE status = "A"
db.users.find( { status: "A" })
SELECT user_id, statusFROM usersWHERE status = "A"
db.users.find( { status: "A" }, { user_id: 1, status: 1, _id: 0 })
SELECT *FROM usersWHERE status != "A"
db.users.find( { status: { $ne: "A" } })
SELECT *FROM usersWHERE status = "A"AND age = 50
db.users.find( { status: "A", age: 50 })
SELECT *FROM usersWHERE status = "A"OR age = 50
db.users.find( { $or: [ { status: "A" } , { age: 50 } ] })
SELECT *FROM usersWHERE age > 25
db.users.find( { age: { $gt: 25 } })
SELECT *FROM usersWHERE age < 25
db.users.find( { age: { $lt: 25 } })
SELECT *FROM usersWHERE age > 25AND age <= 50
db.users.find( { age: { $gt: 25, $lte: 50 } })
SELECT *FROM usersWHERE user_id like "%bc%"
db.users.find( { user_id: /bc/ })
SELECT *FROM usersWHERE user_id like "bc%"
db.users.find( { user_id: /^bc/ })
SELECT *FROM usersWHERE status = "A"ORDER BY user_id ASC
db.users.find( { status: "A" } ).sort( { user_id: 1 } )
SELECT *FROM usersWHERE status = "A"ORDER BY user_id DESC
db.users.find( { status: "A" } ).sort( { user_id: -1 } )
SELECT COUNT(*)FROM users
db.users.count()
or
db.users.find().count()
SELECT COUNT(user_id)FROM users
db.users.count( { user_id: { $exists: true } } )
or
db.users.find( { user_id: { $exists: true } } ).count()
SELECT COUNT(*)FROM usersWHERE age > 30
db.users.count( { age: { $gt: 30 } } )
or
db.users.find( { age: { $gt: 30 } } ).count()
SELECT DISTINCT(status)FROM users
db.users.distinct( "status" )
SELECT *FROM usersLIMIT 1
db.users.findOne()
or
db.users.find().limit(1)
SELECT *FROM usersLIMIT 5SKIP 10
db.users.find().limit(5).skip(10)
EXPLAIN SELECT *FROM usersWHERE status = "A"
db.users.find( { status: "A" } ).explain()
Update Records
The following table presents the various SQL statements related toupdating existing records in tables and the corresponding MongoDBstatements.
UPDATE usersSET status = "C"WHERE age > 25
db.users.update( { age: { $gt: 25 } }, { $set: { status: "C" } }, { multi: true })
UPDATE usersSET age = age + 3WHERE status = "A"
db.users.update( { status: "A" } , { $inc: { age: 3 } }, { multi: true })
Delete Records
The following table presents the various SQL statements related todeleting records from tables and the corresponding MongoDB statements.
DELETE FROM usersWHERE status = "D"
db.users.remove( { status: "D" } )
DELETE FROM users
db.users.remove( )
- Sql与Mongodb 查询映射图
- sql与mongodb操作映射对照
- SQL与mongoDB对比及映射
- MongoDB 教程三: 高级查询 (SQL到MongoDB映射表)
- MongoDB 与 SQL 语句映射关系(持续更新中)
- SQL到MongoDB映射表
- mongodb的查询方式与sql语句对比
- mybatis 源码 -- sql查询映射
- MongoDB 到sql映射表说明
- MONGODB 与sql聚合操作对应图
- mongodb与sql聚合操作对应图
- mongodb与sql聚合对应图 M
- mongodb与sql聚合操作对应图
- MONGODB 与sql聚合操作对应图
- Mongodb 3.2 SQL到MongoDB映射表 官方
- Hibernate_Hibernate映射与查询练习题
- MongoDB Java与对象关系映射
- Mongodb与spring集成 实体映射
- 编写网络程序应该注意的几个问题
- highcharts图例内容过长
- android camera(hal)
- 根据一个字段录入值带出另一个字段值时,有空值的C#.NET处理
- RVDS4.0下 RVISS仿真(2)
- Sql与Mongodb 查询映射图
- 使用搜索引擎的搜索技巧
- 字符设备驱动程序框架
- 纪念一下
- 数据库 闪回
- python 传递的dbus数据类型
- SLF4J入门应用
- Android 通过BroadcastReceiver创建短信接收程序
- 武汉人才服务中心---迁移户口回老家---流程