MongoDB的命令列語句,也可以引用外掛程式LINQ的形式應用

來源:互聯網
上載者:User

 /Files/hanguoji/MongoDB權威指南中文版.rar

MongoDB權威介紹:http://www.cnblogs.com/silentcross/archive/2011/06/27/2091530.html

 

查詢:

MySQL:

SELECT * FROM user

Mongo:

db.user.find()

MySQL:

SELECT * FROM user WHERE name = ’starlee’

Mongo:

db.user.find({‘name’ : ’starlee’})

插入:

MySQL:

INSERT INOT user (`name`, `age`) values (’starlee’,25)

Mongo:

db.user.insert({‘name’ : ’starlee’, ‘age’ : 25})

如果你想在MySQL裡添加一個欄位,你必須:

ALTER TABLE user….

但在MongoDB裡你只需要:

db.user.insert({‘name’ : ’starlee’, ‘age’ : 25, ‘email’ : ’starlee@starlee.com’})

刪除:

MySQL:

DELETE * FROM user

Mongo:

db.user.remove({})

MySQL:

DELETE FROM user WHERE age < 30

Mongo:

db.user.remove({‘age’ : {$lt : 30}})

$gt : > ; $gte : >= ; $lt : < ; $lte : <= ; $ne : !=

更新:

MySQL:

UPDATE user SET `age` = 36 WHERE `name` = ’starlee’

Mongo:

db.user.update({‘name’ : ’starlee’}, {$set : {‘age’ : 36}})

MySQL:

UPDATE user SET `age` = `age` + 3 WHERE `name` = ’starlee’

Mongo:

db.user.update({‘name’ : ’starlee’}, {$inc : {‘age’ : 3}})

MySQL:

SELECT COUNT(*) FROM user WHERE `name` = ’starlee’

Mongo:

db.user.find({‘name’ : ’starlee’}).count()

MySQL:

SELECT * FROM user limit 10,20

Mongo:

db.user.find().skip(10).limit(20)

MySQL:

SELECT * FROM user WHERE `age` IN (25, 35,45)

Mongo:

db.user.find({‘age’ : {$in : [25, 35, 45]}})

MySQL:

SELECT * FROM user ORDER BY age DESC

Mongo:

db.user.find().sort({‘age’ : -1})

MySQL:

SELECT DISTINCT(name) FROM user WHERE age > 20

Mongo:

db.user.distinct(‘name’, {‘age’: {$lt : 20}})

MySQL:

SELECT name, sum(marks) FROM user GROUP BY name

Mongo:

db.user.group({

key : {‘name’ : true},

cond: {‘name’ : ‘foo’},

reduce: function(obj,prev) { prev.msum += obj.marks; },

initial: {msum : 0}

});

MySQL:

SELECT name FROM user WHERE age < 20

Mongo:

db.user.find(‘this.age < 20′, {name : 1})

MongoDB迴圈插入資料:

 

for(var i=0;i<100;i++)db.test.insert({uid:i,uname:’nosqlfan’+i});

上面一次性插入一百條資料,大概結構如下:

{ “_id” : ObjectId(“4c876e519e86023a30dde6b8″), “uid” : 55, “uname” : “nosqlfan55″ }{ “_id” : ObjectId(“4c876e519e86023a30dde6b9″), “uid” : 56, “uname” : “nosqlfan56″ }{ “_id” : ObjectId(“4c876e519e86023a30dde6ba”), “uid” : 57, “uname” : “nosqlfan57″ }{ “_id” : ObjectId(“4c876e519e86023a30dde6bb”), “uid” : 58, “uname” : “nosqlfan58″ }{ “_id” : ObjectId(“4c876e519e86023a30dde6bc”), “uid” : 59, “uname” : “nosqlfan59″ }{ “_id” : ObjectId(“4c876e519e86023a30dde6bd”), “uid” : 60, “uname” : “nosqlfan60″ }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.