java語言

當前位置 /首頁/計算機/java語言/列表

java操作mongodb基礎

複製程式碼 程式碼如下:

java操作mongodb基礎

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型別

Listlist = ray();

tln(());//list的'長度

tln(t());//計算結果的數量,類似於(mysql count()函式),不受limit的影響

//遍歷結果集

while(ext()) {

tln(());

}

}

}

TAG標籤:JAVA mongodb #