Mongodb 基礎

A.Mongodb與mysql語句對比

  MongoDB Mysql
查詢全部 movies.find(new Document()) SELECT * FROM movies
條件查詢 movies.Find(new Document { { "title", "Hello Esr" } }); SELECT * FROM movies WHERE title= 'foobar'
查詢數量 movies.Find(new Document { { "title", "測試2" } }).Documents.Count(); SELECT COUNT(*) FROM movies WHERE `title` = 'foobar'
數量範圍查詢 1, movies.Find(new Document().Add("$where", new Code("this.num > 50")));

2, movies.Find(new Document().Add("num",  new Document().Add("$gt",50)));
($gt : > ; $gte : >= ; $lt : < ; $lte : <= ; $ne : !=)

3,movies.Find("this.num > 50");

4,movies.Find(new Document().Add("$where",new Code("function(x){ return this.num > 50};")));
select * from movies where num > 50
分頁查詢 movies.Find(new Document()).Skip(10).Limit(20); SELECT * FROM movies  limit 10,20
查詢排序語句 movies.Find(new Document()).Sort(new Document() { { "num", -1 } }); SELECT * FROM movies ORDER BY num DESC
查詢指定字段 movies.Find(new Document().Add("num", new Document().Add("$gt", 50)), 10, 0, new Document() { { "title", 1 } }); select title from movies where num > 50
插入語句 movies.Insert(new Document() { { "title", "測試" }, { "resuleData", DateTime.Now } }); INSERT INOT movies (`title`, `reauleDate`) values ('foobar',25)
刪除語句 movies.Remove(new Document() { { "title", "Hello Esr" } }); DELETE * FROM movies
更新語句

movies.Update(new Document() { { "title", "測試2" } }
             , new Document() { { "title", "測試11111" } });

UPDATE movies SET `title` = ‘測試1111’ WHERE `title` = '測試1111'
Linq查詢

(from item in db.GetCollection("movies").Linq()
                       where ((string)item["title"]).StartsWith("Esr")
                       select item);

select * from movies where title li
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章