1. 程式人生 > >SQL to MongoDB

SQL to MongoDB

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.

Executables

The following table presents the MySQL/Oracle executables and the corresponding MongoDB executables.

MySQL/Oracle MongoDB
Database Server mysqld/oracle
Database Client mysql/sqlplus mongo

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
row document or BSON document
column field
index index
table joins embedded documents and linking

primary key

Specify any unique column or column combination as primary key.

In MongoDB, the primary key is automatically set to the _idfield.

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 users.

  • The MongoDB examples assume a collection named users 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 Reference
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 _id is 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 users
ADD join_date DATETIME
Collections do not describe or enforce the structure of the constituent documents. See the Schema Design wiki page for more information. See update() and $set for more information on changing the structure of documents in a collection.
ALTER TABLE users
DROP COLUMN join_date
Collections do not describe or enforce the structure of the constituent documents. See the Schema Design wiki page for more information. See update() and $set for more information on changing the structure of documents in a collection.
CREATE INDEX idx_user_id_asc
ON users(user_id)
db.users.ensureIndex( { user_id: 1 } )
CREATE INDEX
       idx_user_id_asc_age_desc
ON users(user_id, age DESC)
db.users.ensureIndex( { user_id: 1, age: -1 } )
DROP TABLE users
db.users.drop()
See drop() for more information.

Insert

The following table presents the various SQL statements related to inserting records into tables and the corresponding MongoDB statements.

SQL INSERT Statements MongoDB insert() Statements Reference
INSERT INTO users(user_id,
                  age,
                  status)
VALUES ("bcd001",
        45,
        "A")
db.users.insert( {
       user_id: "bcd001",
       age: 45,
       status: "A"
} )
See insert() for more information.

Select

The following table presents the various SQL statements related to reading records from tables and the corresponding MongoDB statements.

SQL SELECT Statements MongoDB find() Statements Reference
SELECT *
FROM users
db.users.find()
See find() for more information.
SELECT id, user_id, status
FROM users
db.users.find(
    { },
    { user_id: 1, status: 1 }
)
See find() for more information.
SELECT user_id, status
FROM users
db.users.find(
    { },
    { user_id: 1, status: 1, _id: 0 }
)
See find() for more information.
SELECT *
FROM users
WHERE status = "A"
db.users.find(
    { status: "A" }
)
See find() for more information.
SELECT user_id, status
FROM users
WHERE status = "A"
db.users.find(
    { status: "A" },
    { user_id: 1, status: 1, _id: 0 }
)
See find() for more information.
SELECT *
FROM users
WHERE status != "A"
db.users.find(
    { status: { $ne: "A" } }
)
See find() and $ne for more information.
SELECT *
FROM users
WHERE status = "A"
AND age = 50
db.users.find(
    { status: "A",
      age: 50 }
)
See find() and $and for more information.
SELECT *
FROM users
WHERE status = "A"
OR age = 50
db.users.find(
    { $or: [ { status: "A" } ,
             { age: 50 } ] }
)
See find() and $or for more information.
SELECT *
FROM users
WHERE age > 25
db.users.find(
    { age: { $gt: 25 } }
)
See find() and $gt for more information.
SELECT *
FROM users
WHERE age < 25
db.users.find(
   { age: { $lt: 25 } }
)
See find() and $lt for more information.
SELECT *
FROM users
WHERE age > 25
AND   age <= 50
db.users.find(
   { age: { $gt: 25, $lte: 50 } }
)
See find()$gt, and $lte for more information.
SELECT *
FROM users
WHERE user_id like "%bc%"
db.users.find(
   { user_id: /bc/ }
)
See find() and $regex for more information.
SELECT *
FROM users
WHERE user_id like "bc%"
db.users.find(
   { user_id: /^bc/ }
)
See find() and $regex for more information.
SELECT *
FROM users
WHERE status = "A"
ORDER BY user_id ASC
db.users.find( { status: "A" } ).sort( { user_id: 1 } )
See find() and sort() for more information.
SELECT *
FROM users
WHERE status = "A"
ORDER BY user_id DESC
db.users.find( { status: "A" } ).sort( { user_id: -1 } )
See find() and sort() for more information.
SELECT COUNT(*)
FROM users
db.users.count()

or

db.users.find().count()
See find() and count() for more information.
SELECT COUNT(user_id)
FROM users
db.users.count( { user_id: { $exists: true } } )

or

db.users.find( { user_id: 
            
           

相關推薦

SQL to MongoDB

In addition to the charts that follow, you might want to consider the Frequently Asked Questions section for a selection of common ques

SQL to Elasticsearch java code

elastics 不支持 sam double group index date days database 把Elasticsearch當成Database用,因為Elasticsearch不支持SQL,就需要把SQL轉換成代碼實現。 1.按某個field group

scrapy相關 Write items to MongoDB

localhost god serve alias md5 package strings win cli 0 1.官網 https://doc.scrapy.org/en/latest/topics/item-pipeline.html#write-items-to-

在zepplin 使用spark sql 查詢mongodb的數據

set int b16 nts CP start lec java_home packages 1.下載zepplin 進入官網下載地址 ,下載完整tar包. 2.解壓 tar zxvf zeppelin-0.7.3.tgz 3.修改配置 新建配置文件 cp

Display Hibernate SQL to console – show_sql , format_sql and use_sql_comments

(轉)靈活控制 Hibernate 的日誌或 SQL 輸出,以便於診斷 - CS408 - 部落格園 https://www.cnblogs.com/lixuwu/p/7479496.html Display Hibernate SQL to console – show_sql , format_sql

Introduction to MongoDB and Python

Python is a powerful programming language used for many different types of applications within the development community. Many know it as a flexible lan

SQL to Java code for Elasticsearch

Elasticsearch雖然定位為Search Engine,但是因其可以持久化資料,很多時候,我們把Elasticsearch當成Database用,但是Elasticsearch不支援SQL,就需要把SQL邏輯轉換成程式碼實現對應的功能。 以下列舉了一些常用的SQL轉換成對應的Java程式碼。 1.

SQL to LinQ 之查詢條件 In的表示(.any)

strQuery = "SELECT OPER, OPER_DESC, OPER_SHORT_DESC FROM WIPOPER WHERE" + " FACTORY = ?"

關於spring-data-mongodb用戶名密碼登錄報錯問題:Failed to authenticate to database

數據 base 設置 thread read ber ram tro pat 一.問題   1.spring-data-mongodb用戶名密碼登錄報錯問題:Failed to authenticate to database org.springframew

My SQL 插入空間數據報錯:Cannot get geometry object from data you send to the GEOMETRY field

ext 點數據 ges cnblogs bject ron rom val images My SQL 插入空間數據報錯:Cannot get geometry object from data you send to the GEOMETRY field 發生該問題,百度

Linq To Sql進階系列(六)用object的動態查詢與保存log篇

directory ont 簡單 lambda表達式 bind add dbo 所有 生成 動態的生成sql語句,根據不同的條件構造不同的where字句,是拼接sql 字符串的好處。而Linq的推出,是為了彌補編程中的 Data != Object 的問題。我們又該如何實現

[SQL] - Attempted to read or write protected memory. This is often an indication that other memory is corrupt. 問題之解決

img png .com 異常 hresult image select att blog 場景: 使用 Oracle.DataAccess.dll 訪問數據庫時,OracleDataAdapter 執行失敗。 異常: System.AccessViolationExce

【總算解決了】A network-related or instance-specific error occurred while establishing a connection to SQL Server

sql tle sqlserve 不知道 程序 tar ace product 自己的 給別人做的網站莫名其妙連接不上數據庫。百度了好多,總算知道自己的錯在哪了。 報 “A network-related or instance-specific error occ

LINQ TO SQL和Entity Framework 的關系 你了解多少?

mode 最小 -m 發的 開發 content 內容 語言 account 1. LINQ TO SQL 和EF 特點: LINQ TO SQL和Entity Framework都是一種包含LINQ功能的ORM 也就是所謂的關系對象的映射。其中包括的有DBFrist

mongodb Failed to start LSB: An object/document-oriented dat

failed art var col document class bsp chown sta 解決辦法: 1 cd /var/lib 2 sudo rm -rf ./mongodb 3 sudo mkdir mongodb 4 sudo chown -R mon

SQL SERVER – Configuration Manager – Cannot Connect to WMI Provider. You Do Not Have Permission or The Server is Unreachable

hab ssi ima onf wmi manager ges ger connect 打開SQL SERVER Configuarion Manger 出現以下錯誤 SQL Server Configuration Manager—————————Cannot conn

A Sample To use Update, InnerJoin, When/Case, Exists in SQL Server

div enc sin date() end arc inner white from DECLARE @SINKKNO varchar(11),@KYOKAYMD varchar(8),@LASTUPDATEID varchar(5),@LASTUPDPGID varch

翻譯(三)Stairway to T-SQL: Beyond The Basics Level 9: Dynamic T-SQL Code

數據庫表 studio 應用程序 cmd char 結束 管理 分代 應對 Stairway to T-SQL: Beyond The Basics Level 9: Dynamic T-SQL Code By Gregory Larsen, 2016/07/

PL/SQL Developer登錄出現——Using a filter for all users can lead to poor performance!

objects default devel http mage eve 配置 tool cnblogs 用PL/SQL Developer登錄Oracle時提示:Using a filter for all users can lead to poor performan

Linq to SQL 的連表查詢(轉)

equal query 交集 數據庫 調用 數據 變量 bst log 關於數據庫的查詢中經常需要用到多表的連接查詢,這裏就簡單地展示關於linq的查詢功能。 1、單表的查詢 [csharp] view plain copy var query = from