Showing posts with label Mongo. Show all posts
Showing posts with label Mongo. Show all posts

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方法




11/12/2012

JAVA AND MONGODB CONNECTION

開啟eclipse


Create a Project: File -> New -> Project


create a Project name (ex. testNHIRD)


 Create a Package under src of Java Project (ex. testNHIRD)


Give a name for Package (ex. mongoConnection)


Create Class


Give a name for Class (ex. JavaMongoDBConnection)


The screen look like the following picture


Use "add external archives" to add Java and MongoDB driver

Please see ADD EXTERNAL ARCHIVES

Then type codes as follows
You can also take Making A Connection from MongoDB tutorial website as a reference





Other useful websites

Java Tutorial of MongoDB

Java + MongoDB Hello World Example