mongodb文法備份

來源:互聯網
上載者:User

標籤:

 mongodb文法

 

MongoDB的好處挺多的,比如多列索引,查詢時可以用一些統計函數,支援多條件查詢,但是目前多表查詢是不支援的,可以想辦法通過資料冗餘來解決多表查詢的問題。

 

 

查詢colls所有資料

db.colls.find() //select * from colls

 

通過指定條件查詢

db.colls.find({‘last_name’: ‘Smith’});//select * from colls where last_name=’Smith’

 

指定多條件查詢

db.colls.find( { x : 3, y : “foo” } );//select * from colls where x=3 and y=’foo’

 

指定條件範圍查詢

db.colls.find({j: {$ne: 3}, k: {$gt: 10} });//select * from colls where j!=3 and k>10

 

查詢不包括某內容

db.colls.find({}, {a:0});//查詢除a為0外的所有資料

 

支援<, <=, >, >=查詢,需用符號替代分別為$lt,$lte,$gt,$gte

db.colls.find({ “field” : { $gt: value } } ); 

db.colls.find({ “field” : { $lt: value } } ); 

db.colls.find({ “field” : { $gte: value } } );

db.colls.find({ “field” : { $lte: value } } );

 

也可對某一欄位做範圍查詢

db.colls.find({ “field” : { $gt: value1, $lt: value2 } } );

 

不等於查詢用字元$ne

db.colls.find( { x : { $ne : 3 } } );

 

in查詢用字元$in

db.colls.find( { “field” : { $in : array } } );

db.colls.find({j:{$in: [2,4,6]}});

 

not in查詢用字元$nin

db.colls.find({j:{$nin: [2,4,6]}});

 

模數查詢用字元$mod

db.colls.find( { a : { $mod : [ 10 , 1 ] } } )// where a % 10 == 1

 

$all查詢

db.colls.find( { a: { $all: [ 2, 3 ] } } );//指定a滿足數組中任意值時

 

$size查詢

db.colls.find( { a : { $size: 1 } } );//對對象的數量查詢,此查詢查詢a的子物件數目為1的記錄

 

$exists查詢

db.colls.find( { a : { $exists : true } } ); // 存在a對象的資料

db.colls.find( { a : { $exists : false } } ); // 不存在a對象的資料

 

$type查詢$type值為bsonhttp://bsonspec.org/數 據的類型值

db.colls.find( { a : { $type : 2 } } ); // 匹配a為string類型資料

db.colls.find( { a : { $type : 16 } } ); // 匹配a為int類型資料

 

使用Regex匹配

db.colls.find( { name : /acme.*corp/i } );//類似於SQL中like

 

內嵌物件查詢

db.colls.find( { “author.name” : “joe” } );

 

1.3.3版本及更高版本包含$not查詢

db.colls.find( { name : { $not : /acme.*corp/i } } );

db.colls.find( { a : { $not : { $mod : [ 10 , 1 ] } } } );

 

sort()排序

db.colls.find().sort( { ts : -1 } );//1為升序2為降序

 

limit()對限制查詢資料返回個數

db.colls.find().limit(10)

 

skip()跳過某些資料

db.colls.find().skip(10)

 

snapshot()快照保證沒有重複資料返回或對象丟失

 

count()統計查詢對象個數

db.students.find({‘address.state’ : ‘CA’}).count();//效率較高

db.students.find({‘address.state’ : ‘CA’}).toArray().length;//效率很低

 

group()對查詢結果分組和SQL中group by函數類似

distinct()返回不重複值

mongodb文法備份(轉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.