On this page本页内容
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.除了下面的图表之外,您还可能需要考虑常见问题部分来选择关于MunGDB的常见问题。
The following table presents the various SQL terminology and concepts and the corresponding MongoDB terminology and concepts.下表列出了各种SQL术语和概念以及相应的MongoDB术语和概念。
$lookup | |
|
|
GROUP BY) |
|
SELECT INTO NEW_TABLE |
|
MERGE INTO TABLE |
|
UNION ALL | $unionWith |
|
|
The following table presents some database executables and the corresponding MongoDB executables. 下表显示了一些数据库可执行文件和相应的MongoDB可执行文件。This table is not meant to be exhaustive.本表并非详尽无遗。
| MongoDB | MySQL | Oracle | Informix | DB2 | |
|---|---|---|---|---|---|
mongod | mysqld | oracle | IDS | DB2 Server | |
mongosh | mysql | sqlplus | DB-Access | DB2 Client |
The following table presents the various SQL statements and the corresponding MongoDB statements. 下表显示了各种SQL语句和相应的MongoDB语句。The examples in the table assume the following conditions:表中的示例假设以下条件:
people.people的表。The MongoDB examples assume a collection named MongoDB示例假定一个名为people that contain documents of the following prototype:people的集合包含以下原型的文档:
{
_id: ObjectId("509a8fb2f3f4948bd2f983a0"),
user_id: "abc123",
age: 55,
status: 'A'
}
The following table presents the various SQL statements related to table-level actions and the corresponding MongoDB statements.下表显示了与表级操作相关的各种SQL语句以及相应的MongoDB语句。
| MongoDB | |
|---|---|
CREATE TABLE people ( id MEDIUMINT NOT NULL AUTO_INCREMENT, user_id Varchar(30), age Number, status char(1), PRIMARY KEY (id) ) |
db.people.insertOne( {
user_id: "abc123",
age: 55,
status: "A"
} )
db.createCollection("people")
|
ALTER TABLE people ADD join_date DATETIME |
db.people.updateMany(
{ },
{ $set: { join_date: new Date() } }
)
|
ALTER TABLE people DROP COLUMN join_date |
db.people.updateMany(
{ },
{ $unset: { "join_date": "" } }
)
|
CREATE INDEX idx_user_id_asc ON people(user_id) | db.people.createIndex( { user_id: 1 } )
|
CREATE INDEX idx_user_id_asc_age_desc ON people(user_id, age DESC) | db.people.createIndex( { user_id: 1, age: -1 } )
|
DROP TABLE people | db.people.drop() |
For more information on the methods and operators used, see:有关使用的方法和运算符的更多信息,请参阅:
db.collection.insertOne()db.collection.insertMany()db.createCollection()db.collection.updateMany()db.collection.createIndex()db.collection.drop()$set$unsetThe following table presents the various SQL statements related to inserting records into tables and the corresponding MongoDB statements.下表显示了与向表中插入记录相关的各种SQL语句以及相应的MongoDB语句。
SQL INSERT Statements | insertOne()语句 |
|---|---|
INSERT INTO people(user_id, age, status) VALUES ("bcd001", 45, "A") | db.people.insertOne(
{ user_id: "bcd001", age: 45, status: "A" }
)
|
For more information, see 有关更多信息,请参阅db.collection.insertOne().db.collection.insertOne()。
The following table presents the various SQL statements related to reading records from tables and the corresponding MongoDB statements.下表显示了与从表中读取记录相关的各种SQL语句以及相应的MongoDB语句。
The 除非通过投影明确排除,否则find() method always includes the _id field in the returned documents unless specifically excluded through projection. find()方法始终在返回的文档中包含_id字段。Some of the SQL queries below may include an 下面的一些SQL查询可能包含一个_id field to reflect this, even if the field is not included in the corresponding find() query._id字段来反映这一点,即使该字段未包含在相应的find()查询中。
SQL SELECT | find()语句 |
|---|---|
SELECT * FROM people | db.people.find() |
SELECT id, user_id, status FROM people | db.people.find(
{ },
{ user_id: 1, status: 1 }
)
|
SELECT user_id, status FROM people | db.people.find(
{ },
{ user_id: 1, status: 1, _id: 0 }
)
|
SELECT * FROM people WHERE status = "A" | db.people.find(
{ status: "A" }
)
|
SELECT user_id, status FROM people WHERE status = "A" | db.people.find(
{ status: "A" },
{ user_id: 1, status: 1, _id: 0 }
)
|
SELECT * FROM people WHERE status != "A" | db.people.find(
{ status: { $ne: "A" } }
)
|
SELECT * FROM people WHERE status = "A" AND age = 50 | db.people.find(
{ status: "A",
age: 50 }
)
|
SELECT * FROM people WHERE status = "A" OR age = 50 | db.people.find(
{ $or: [ { status: "A" } , { age: 50 } ] }
)
|
SELECT * FROM people WHERE age > 25 | db.people.find(
{ age: { $gt: 25 } }
)
|
SELECT * FROM people WHERE age < 25 | db.people.find(
{ age: { $lt: 25 } }
)
|
SELECT * FROM people WHERE age > 25 AND age <= 50 | db.people.find(
{ age: { $gt: 25, $lte: 50 } }
)
|
SELECT * FROM people WHERE user_id like "%bc%" | db.people.find( { user_id: /bc/ } )
db.people.find( { user_id: { $regex: /bc/ } } )
|
SELECT * FROM people WHERE user_id like "bc%" | db.people.find( { user_id: /^bc/ } )
db.people.find( { user_id: { $regex: /^bc/ } } )
|
SELECT * FROM people WHERE status = "A" ORDER BY user_id ASC | db.people.find( { status: "A" } ).sort( { user_id: 1 } )
|
SELECT * FROM people WHERE status = "A" ORDER BY user_id DESC | db.people.find( { status: "A" } ).sort( { user_id: -1 } )
|
SELECT COUNT(*) FROM people | db.people.count()
db.people.find().count() |
SELECT COUNT(user_id) FROM people | db.people.count( { user_id: { $exists: true } } )
db.people.find( { user_id: { $exists: true } } ).count()
|
SELECT COUNT(*) FROM people WHERE age > 30 | db.people.count( { age: { $gt: 30 } } )
db.people.find( { age: { $gt: 30 } } ).count()
|
SELECT DISTINCT(status) FROM people | db.people.aggregate( [ { $group : { _id : "$status" } } ] )
db.people.distinct( "status" )
|
SELECT * FROM people LIMIT 1 | db.people.findOne()
db.people.find().limit(1)
|
SELECT * FROM people LIMIT 5 SKIP 10 | db.people.find().limit(5).skip(10) |
EXPLAIN SELECT * FROM people WHERE status = "A" | db.people.find( { status: "A" } ).explain()
|
For more information on the methods and operators used, see有关使用的方法和运算符的更多信息,请参阅
db.collection.find()db.collection.distinct()db.collection.findOne()limit()skip()explain()sort()count()$ne$and$or$gt$lt$exists$lte$regexThe following table presents the various SQL statements related to updating existing records in tables and the corresponding MongoDB statements.下表显示了与更新表中现有记录相关的各种SQL语句以及相应的MongoDB语句。
updateMany()语句 | |
|---|---|
UPDATE people SET status = "C" WHERE age > 25 | db.people.updateMany(
{ age: { $gt: 25 } },
{ $set: { status: "C" } }
)
|
UPDATE people SET age = age + 3 WHERE status = "A" | db.people.updateMany(
{ status: "A" } ,
{ $inc: { age: 3 } }
)
|
For more information on the method and operators used in the examples, see:有关示例中使用的方法和运算符的更多信息,请参阅:
The following table presents the various SQL statements related to deleting records from tables and the corresponding MongoDB statements.下表显示了与从表中删除记录相关的各种SQL语句以及相应的MongoDB语句。
deleteMany()语句 | |
|---|---|
DELETE FROM people WHERE status = "D" | db.people.deleteMany( { status: "D" } )
|
DELETE FROM people | db.people.deleteMany({})
|
For more information, see 有关更多信息,请参阅db.collection.deleteMany().db.collection.deleteMany()。
If you are considering migrating your SQL application to MongoDB, download the MongoDB Application Modernization Guide.如果您正在考虑将SQL应用程序迁移到MongoDB,请下载MongoDB应用程序现代化指南。
The download includes the following resources:下载内容包括以下资源: