首页 > 代码库 > java操作mongodb
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