java操作mongodb
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java操作mongodb相关的知识,希望对你有一定的参考价值。
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的主要内容,如果未能解决你的问题,请参考以下文章