有关关系型数据库跟Mongod的语法对比
In addition to the charts that follow, you might want to consider the Frequently Asked Questions section for a selection of common questions about MongoDB.
Terminology and Concepts
The following table presents the various SQL terminology and concepts and the corresponding MongoDB terminology and concepts.
SQL Terms/Concepts | MongoDB Terms/Concepts |
---|---|
database | database |
table | collection |
row | document or BSON document |
column | field |
index | index |
table joins |
$lookup , embedded documents |
primary key Specify any unique column or column combination as primary key. |
In MongoDB, the primary key is automatically set to the _idfield. |
aggregation (e.g. group by) |
aggregation pipeline See the SQL to Aggregation Mapping Chart. |
Executables
The following table presents some database executables and the corresponding MongoDB executables. This table is not meant to be exhaustive.
MongoDB | MySQL | Oracle | Informix | DB2 | |
---|---|---|---|---|---|
Database Server | mongod |
mysqld |
oracle |
IDS |
DB2 Server |
Database Client | mongo |
mysql |
sqlplus |
DB-Access |
DB2 Client |
Examples
The following table presents the various SQL statements and the corresponding MongoDB statements. The examples in the table assume the following conditions:
The SQL examples assume a table named
people
.-
The MongoDB examples assume a collection named
people
that contain documents 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 to table-level actions and the corresponding MongoDB statements.
SQL Schema Statements | MongoDB Schema Statements |
---|---|
CREATE TABLE people ( |
Implicitly created on first db.people.insertOne( { However, you can also explicitly create a collection: db.createCollection("people") |
ALTER TABLE people |
Collections do not describe or enforce the structure of its documents; i.e. there is no structural alteration at the collection level. However, at the document level, db.people.updateMany( |
ALTER TABLE people |
Collections do not describe or enforce the structure of its documents; i.e. there is no structural alteration at the collection level. However, at the document level, db.people.updateMany( |
CREATE INDEX idx_user_id_asc |
db.people.createIndex( { user_id: 1 } ) |
CREATE INDEX |
db.people.createIndex( { user_id: 1, age: -1 } ) |
DROP TABLE people |
db.people.drop() |
For more information, see:
Insert
The following table presents the various SQL statements related to inserting records into tables and the corresponding MongoDB statements.
SQL INSERT Statements | MongoDB insertOne() Statements |
---|---|
INSERT INTO people(user_id, |
db.people.insertOne( |
For more information, see db.collection.insertOne()
.
Select
The following table presents the various SQL statements related to reading records from tables and the corresponding MongoDB statements.
NOTE
The find()
method always includes the _id
field in the returned documents unless specifically excluded through projection. Some of the SQL queries below may include an _id
field to reflect this, even if the field is not included in the corresponding find()
query.
SQL SELECT Statements | MongoDB find() Statements |
---|---|
SELECT * |
db.people.find() |
SELECT id, |
db.people.find( |
SELECT user_id, status |
db.people.find( |
SELECT * |
db.people.find( |
SELECT user_id, status |
db.people.find( |
SELECT * |
db.people.find( |
SELECT * |
db.people.find( |
SELECT * |
db.people.find( |
SELECT * |
db.people.find( |
SELECT * |
db.people.find( |
SELECT * |
db.people.find( |
SELECT * |
db.people.find( { user_id: /bc/ } ) -or- db.people.find( { user_id: { $regex: /bc/ } } ) |
SELECT * |
db.people.find( { user_id: /^bc/ } ) -or- db.people.find( { user_id: { $regex: /^bc/ } } ) |
SELECT * |
db.people.find( { status: "A" } ).sort( { user_id: 1 } ) |
SELECT * |
db.people.find( { status: "A" } ).sort( { user_id: -1 } ) |
SELECT COUNT(*) |
db.people.count() or db.people.find().count() |
SELECT COUNT(user_id) |
db.people.count( { user_id: { $exists: true } } ) or db.people.find( { user_id: { $exists: true } } ).count() |
SELECT COUNT(*) |
db.people.count( { age: { $gt: 30 } } ) or db.people.find( { age: { $gt: 30 } } ).count() |
SELECT DISTINCT(status) |
db.people.distinct( "status" ) |
SELECT * |
db.people.findOne() or db.people.find().limit(1) |
SELECT * |
db.people.find().limit(5).skip(10) |
EXPLAIN SELECT * |
db.people.find( { status: "A" } ).explain() |
For more information, see:
Update Records
The following table presents the various SQL statements related to updating existing records in tables and the corresponding MongoDB statements.
SQL Update Statements | MongoDB updateMany() Statements |
---|---|
UPDATE people |
db.people.updateMany( |
UPDATE people |
db.people.updateMany( |
For more information, see db.collection.updateMany()
, $set
, $inc
, and $gt
.
Delete Records
The following table presents the various SQL statements related to deleting records from tables and the corresponding MongoDB statements.
SQL Delete Statements | MongoDB deleteMany() Statements |
---|---|
DELETE FROM people |
db.people.deleteMany( { status: "D" } ) |
DELETE FROM people |
db.people.deleteMany({}) |
For more information, see db.collection.deleteMany()
.