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(());

}

}

}