码迷,mamicode.com
首页 > 数据库 > 详细

java操作mongodb

时间:2016-09-14 16:36:53      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:

package pymongo1;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;

public class m1 {
public String url;
public int port;
public String dbname;


//public static String collection_name="DeviceData";
MongoClient mongoClient;
DB db;

public static void main(String[] args) {
// TODO Auto-generated method stub


}
public m1(String url,int port,String dbname)
{
this.url=url;
this.port=port;
this.dbname=dbname;
}
public DB conn()
{
mongoClient = new MongoClient(url , port );
db = mongoClient.getDB( dbname );
return db;

}
public DBCursor m2(String collection_name){
DBCursor cursor=null;
try{


DBCollection coll = db.getCollection(collection_name);
cursor=coll.find();
// while(cursor.hasNext()){System.out.println(cursor.next());}
//


}catch(Exception e){e.printStackTrace();}
return cursor;
}


public DBCursor m2(String collection_name,DBObject ob)
{
DBCursor cursor=null;
try{


DBCollection coll = db.getCollection(collection_name);
cursor=coll.find(ob);

// while(cursor.hasNext()){System.out.println(cursor.next());}
//


}catch(Exception e){e.printStackTrace();}
return cursor;


}


public DBObject m3(String collection_name,BasicDBObject ob,String flag)
{
DBObject cursor=null;

try{


DBCollection coll = db.getCollection(collection_name);
// cursor=coll.find(ob);
cursor=coll.findOne(ob);
// while(cursor.hasNext()){System.out.println(cursor.next());}
//


}catch(Exception e){e.printStackTrace();}
return cursor;


}
}

java操作mongodb

标签:

原文地址:http://www.cnblogs.com/luo-mao/p/5872570.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!