複製代碼 代碼如下:
package oot;
import ;
import cDBObject;
import ;
import llection;
import rsor;
import ject;
import oClient;
public class test_mongodb {
public static void main(String args[]) throws Exception {
MongoClient mongoClient = new MongoClient( "" , 27017 );//建立連接
DB get_db_credit = B("credit_2");//數據庫名
DBCollection collection = get_db_ollection("report");//集合名,對應mysql中的表名
BasicDBObject filter_dbobject = new BasicDBObject();
//建立查詢條件,如果還有其他條件,類似的寫即可
// 如:version=3,filter_("version", 3),mongod區分String 和 Integer類型,所以要小心"3"!=3
filter_("user_id", "10065716153075");
//下面執行查詢,設置limit,只要10條數據,排序(類mysql orderby) 再建一個BasicDBObject即可,-1表示倒序
DBCursor cursor = (filter_dbobject)t(10)(new BasicDBObject("create_time",-1));
//把結果集輸出成list類型
List
tln(());//list的'長度
tln(t());//計算結果的數量,類似於(mysql count()函數),不受limit的影響
//遍歷結果集
while(ext()) {
tln(());
}
}
}