mongo与关系型数据库的使用对比

<strong>一、执行程序的区别:</strong>

 MySQL executable

Oracle executable 

 Mongo executable

 mysqld

 oracle

 mongod

 mysql

 sqlplus

 mongo

<strong>二、库、表、行、列等概念的区别:</strong>



&nbsp;
<table style="width:37.62%;height:226px;" border="1" cellspacing="0" bordercolor="#000000" cellpadding="2">
    <tbody>
        <tr>
            <td>
                &nbsp;MySQL term
            </td>
            <td>
                &nbsp;Mongo term/concept
            </td>
        </tr>
        <tr>
            <td>
                &nbsp;database
            </td>
            <td>
                &nbsp;database
            </td>
        </tr>
        <tr>
            <td>
                &nbsp;table
            </td>
            <td>
                &nbsp;collection
            </td>
        </tr>
        <tr>
            <td>
                &nbsp;index
            </td>
            <td>
                &nbsp;index
            </td>
        </tr>
        <tr>
            <td>
                &nbsp;row
            </td>
            <td>
                &nbsp;BSON document<span style="display:none;" id="__kindeditor_bookmark_end_180__"></span>
            </td>
        </tr>
        <tr>
            <td>
                &nbsp;column
            </td>
            <td>
                &nbsp;BSON field
            </td>
        </tr>
        <tr>
            <td>
                &nbsp;join
            </td>
            <td>
                &nbsp;embedding and linking
            </td>
        </tr>
        <tr>
            <td>
                &nbsp;primary key
            </td>
            <td>
                &nbsp;_id field
            </td>
        </tr>
        <tr>
            <td>
                &nbsp;group by
            </td>
            <td>
                &nbsp;<a title="SQL to Aggregation Framework Mapping Chart" href="http://www.mongodb.org/display/DOCS/SQL+to+Aggregation+Framework+Mapping+Chart" target="_blank" rel="noopener">aggregation</a>
            </td>
        </tr>
    </tbody>
</table>



<strong>三、语句用法上的区别:</strong>
 SQL Statement  Mongo Statement
 CREATE TABLE USERS (a Number, b Number)  db.createCollection(“mycoll”)
 ALTER TABLE users ADD …  implicit
 INSERT INTO USERS VALUES(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’});
&nbsp;



&nbsp;以上内容参考<a href="http://www.mongodb.org/display/DOCS/SQL+to+Mongo+Mapping+Chart" target="_blank" rel="noopener">mongo官方文章。</a>

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注