6/18/2013

Mongo Math

以下介紹一些常用到的指令
$gt: greater than 大於           $gte: greater than and equal to 大於等於      
$lt: less than       小於           $lte: greater than and equal to 小於等於

以下用到的練習的collection名稱為school (學校)
document(文檔)包括: department(系所),  professor(教授姓名), student(學生姓名),  field(研究領域), grade(年級), point(學分數)

Greater than 大於

在Mongo Shell裡可以用db.school.find ({ "point" : {$gt : 20} });
例如   搜尋學分數大於20的學生資料
下列練習可以發現command line將學分數為40, 33, 25, 36的紀錄都列出來


在Java中若要找學分數大於30的紀錄,可使用
DBCollection coll = db.getCollection("Collection Name");
DBObject query = 
new QueryBuilder().put("Document Name").greaterThan(Value).get();

DBCollection coll = db.getCollection("school");
DBObject query = new QueryBuilder().put("point").greaterThan(33).get();

Greater than and equal to 大於等於

在Mongo shell裡面使用 $gte,在Java則使用
DBObject query = 
new QueryBuilder().put("document name").greaterThanEquals(Value).get();
在Concole可以看到把學分數(point)為40, 33, 46的紀錄都列出來了


Less than 小於

類似Greater的語法  這裡假設找出學分數小於33的紀錄
使用DBObject query = 
new QueryBuilder().put("point").lessThan(33).get();


Less than and euqal to 小於等於

在Mongo shell可使用$lte,在Java則使用lessThanEquals(Value)
DBObject query = new QueryBuilder().put("point").lessThanEquals(33).get();
這個練習把學分數等於33的紀錄也列出來


下圖分別列出小於20跟大於20的練習


介於兩數值之間

若要查詢介於某個數值跟某個數值之間
例如大於20且小於40
則可用    db.school.find ({ "point" : {$gt : 20,  $lt:40} });




6/15/2013

Mongo Advanced Search

In my test databasse, 
collection: testPatientInfo
document: testName (姓名), testID (身份證字號), testAge (年齡), testAddress (地址)
embedded document (內嵌文檔) of testAddress are: testNo (門牌號碼), testStreet (路名), testCity (城市), testCountry (國家), zip code (郵遞區號)

We are going to search the patient who lives in Taipei City, then we can use
db.Collection.find( { "Document.embedded document" : Value} );
In Mongo shell, the we can type
db.testPatientInfo.find( { "testAddress.testCity" : "Taipei"});



交集/ A且B

找學生為四年級且有40學分的紀錄
the student who is in 4th grade and has 40 points
由結果發現找出一筆符合的紀錄


找出特定搜尋條件的紀錄

找出學生姓名(student)為Hsu的學生的紀錄
DBObject query = new QueryBuilder().put("grade").is(4).get();


Not equals 

找出學生姓名不為Hsu的紀錄
DBObject query = new QueryBuilder().put("student").notEquals("Hsu").get();


$in/ in(inList)方法

db.CollectionName.find( {Document Name: [value1, value2, ...]})
找school表中學分數(point)為8, 13, 40紀錄
db.school.find( {point: [8, 13, 40]} );




$nin/ notIn(inList)

在Mongo shell中用 $nin,列出不包含這些資料的紀錄

在Java中可用notIn(inList)

skip方法




Mongo Basic Search

以下是Mongo shell簡易查詢的一些練習
我先自行建立虛擬的學生資料
collection為school (學校)
document(文檔)包括: department(系所),  professor(教授姓名), student(學生姓名),  field(研究領域), grade(年級), point(學分數)

升冪與降冪

若要將某文檔的數值由小到大(升冪)或由大到小(降冪)排列
例如:將年級升冪排列
db.school.find().sort( {grade: 1} );
查詢到的學生資料,可以發現他們的年級依序為1, 2, 3, 3, 4, 4

若是將年級降冪排列,則把上倏地{grade: 1}改為{grade: -1}
db.school.find().sort( {grade: -1} );
查詢到的學生資料,可以發現他們的年級依序為4, 4, 3, 3, 2, 1



更換document的值

假設有學生的名字誤值, 要更換學生姓名,可用$set方法,並用update更新database
以下我們把學生姓名為Chang的資料改為Monday
db.school.update({student : "Chang"}, {$set: {"student" : "Monday"}});
















以上練習參考自MongoDB管理與開發精要(機械工業出版社)    並搭配自己使用的範例做練習